C#中如何为comboBox和textBox绑定SQL数据库指定字段名(求具体代码!)

2024-12-22 15:48:41
推荐回答(3个)
回答1:

comboBox:
string con = "Server=localhost;database=Test;uid=sa;pwd=sa12345";
SqlConnection conn = new SqlConnection(con);
conn.Open();
string sql = "select stuName from StudentInfo where score<60";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
comboBox1.DataSource = dt;
textBox:
string con = "Server=localhost;database=Test;uid=sa;pwd=sa12345";
SqlConnection conn = new SqlConnection(con);
conn.Open();
string sql = "select info from StudentInfo where score<60";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
textBox1.Text = dt.Rows[0][0].ToString();

回答2:

SQL语句的条件变了一下

回答3:

用vs中工具箱的数据中的BindingSource控件来绑定,去百度查一下如何使用这个控件