日期:2014-05-18  浏览次数:21014 次

C#Winform中怎样用SqlDataAdapter把绑定到TextBox的数据更新到数据库
从数据库中用SqlDataAdapter去的DataTable后,我把这张表绑定到了几个TextBox中,发现一个问题:
通过textBox更新的值等反应到DataTable中,但是无法更新到数据库中,但是如果绑定到dataGridView就可以
private void button1_Click(object sender, EventArgs e)
  {
  SqlDataAdapter1 = new SqlDataAdapter("select * from test",DatabaseManager.getConn());
  SqlDataAdapter1.Fill(Table1);
  textBox1.DataBindings.Add("Text",Table1,"ID"); //绑定到textBox
  textBox2.DataBindings.Add("Text", Table1, "Names");
  textBox3.DataBindings.Add("Text", Table1, "vvv");

  }



  private void button2_Click(object sender, EventArgs e)
  {
  for (int i = 0; i < 3; i++) //这里确认了,TextBox的值确实提交到了DataTable中
  Console.WriteLine(Table1.Rows[0][i].ToString());
  SqlCommandBuilder b = new SqlCommandBuilder(SqlDataAdapter1);
  SqlDataAdapter1.Update(Table1);
  }

------解决方案--------------------
textBox1.DataBindings.Add("Text",myTable,"id");

textBox1.DataBindings["Text"].WriteValue();

myCurrencyManager = (CurrencyManager)this.BindingContext[myTable];
myCurrencyManager.Position = 0;
dataGrid1.DataSource=myTable;