示例代码如下:
using (SqlConnection conn = new SqlConnection(Common.strConn))
{
conn.Open();
sc.Connection = conn; sc.CommandText = "SELECT * FROM SORDER";
sda.SelectCommand = sc;
sda.Fill(ds,"SORDER");
conn.Close();
}
TextBox tx1=new TextBox();
tx1.Loction=new Point(0,0);
tx1.Databinding.Add("Text",ds,"SORDER.SOHNUM");
This.Controls.Add(tx1); TextBox tx2=new TextBox();
tx2.Loction=new Point(0,20);
tx2.Databinding.Add("Text",ds,"SORDER.ORDDAT");
This.Controls.Add(tx2); TextBox tx3=new TextBox();
tx3.Loction=new Point(0,30);
tx3.Databinding.Add("Text",ds,"SORDER.BPCNUM");
This.Controls.Add(tx3); 问题是,我在界面修改了数据,表中的数据好像并没有跟着修改。然后我要怎么删除、添加、修改数据呢?
using (SqlConnection conn = new SqlConnection(Common.strConn))
{
conn.Open();
sc.Connection = conn; sc.CommandText = "SELECT * FROM SORDER";
sda.SelectCommand = sc;
sda.Fill(ds,"SORDER");
conn.Close();
}
TextBox tx1=new TextBox();
tx1.Loction=new Point(0,0);
tx1.Databinding.Add("Text",ds,"SORDER.SOHNUM");
This.Controls.Add(tx1); TextBox tx2=new TextBox();
tx2.Loction=new Point(0,20);
tx2.Databinding.Add("Text",ds,"SORDER.ORDDAT");
This.Controls.Add(tx2); TextBox tx3=new TextBox();
tx3.Loction=new Point(0,30);
tx3.Databinding.Add("Text",ds,"SORDER.BPCNUM");
This.Controls.Add(tx3); 问题是,我在界面修改了数据,表中的数据好像并没有跟着修改。然后我要怎么删除、添加、修改数据呢?
只知道自己写代码更新
加一个button,在button事件中写更新方法
private void Form2_Load(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection("server=localhost;uid=sa;pwd=;database=pubs");
SqlDataAdapter ad=new SqlDataAdapter("select * from authors",cn);
myTable=new DataTable();
ad.Fill(myTable);
textBox1.DataBindings.Add("Text",myTable,"au_id");
textBox2.DataBindings.Add("Text",myTable,"au_fname");
textBox3.DataBindings.Add("Text",myTable,"au_lname");
textBox4.DataBindings.Add("Text",myTable,"phone");
textBox5.DataBindings.Add("Text",myTable,"address");
textBox6.DataBindings.Add("Text",myTable,"city");
textBox7.DataBindings.Add("Text",myTable,"state");
textBox8.DataBindings.Add("Text",myTable,"zip"); Binding b=checkBox1.DataBindings.Add("Checked",myTable,"contract");
b.Parse+=new ConvertEventHandler(b_Parse);
b.Format+=new ConvertEventHandler(b_Format);
myCurrencyManager = (CurrencyManager)this.BindingContext[myTable];
myCurrencyManager.Position = 0;
dataGrid1.DataSource=myTable;
}
给你看看我做的绑定,没有问题,很好用的。
用SqlDataAdapter.Updata(Dataset ds)