我从图型界面拉出来的一格,自动生成的
this.tABLE1TableAdapter3.Fill(this.testDataSet3.TABLE1);
这样的数据集我在update()的时候会把插入行的ID一起更新.而我用手动写的
System.Data.SqlClient.SqlConnection Sqlconn = new System.Data.SqlClient.SqlConnection();
Sqlconn.ConnectionString = "Data Source=JAKING;Initial Catalog=test;User ID=sa;Password=12345";
System.Data.SqlClient.SqlCommand Sqlcomm = new System.Data.SqlClient.SqlCommand();
Sqlcomm.Connection = Sqlconn;
Sqlcomm.CommandText = "SELECT aaaa,id FROM TABLE1";
SQLDA.SelectCommand = Sqlcomm;
DS.Tables.Add(dt );
SQLDA.Fill(dt);
System.Data.SqlClient.SqlCommandBuilder SqlDB = new System.Data.SqlClient.SqlCommandBuilder(SQLDA); SQLDA.Update(dt);
更新的时候为什么不能把主键的ID从数据库中更新回来.是在插入的时候主键是自动增加的.
this.tABLE1TableAdapter3.Fill(this.testDataSet3.TABLE1);
这样的数据集我在update()的时候会把插入行的ID一起更新.而我用手动写的
System.Data.SqlClient.SqlConnection Sqlconn = new System.Data.SqlClient.SqlConnection();
Sqlconn.ConnectionString = "Data Source=JAKING;Initial Catalog=test;User ID=sa;Password=12345";
System.Data.SqlClient.SqlCommand Sqlcomm = new System.Data.SqlClient.SqlCommand();
Sqlcomm.Connection = Sqlconn;
Sqlcomm.CommandText = "SELECT aaaa,id FROM TABLE1";
SQLDA.SelectCommand = Sqlcomm;
DS.Tables.Add(dt );
SQLDA.Fill(dt);
System.Data.SqlClient.SqlCommandBuilder SqlDB = new System.Data.SqlClient.SqlCommandBuilder(SQLDA); SQLDA.Update(dt);
更新的时候为什么不能把主键的ID从数据库中更新回来.是在插入的时候主键是自动增加的.
Col(主键自动加1) Col2
1 abc
2 efg
---------------------------
我的datatable的操作为新增一行1 abc
2 efg
6 kkkk(新增的行)
我在运行update()行,如果是自动的代码就会变为
1 abc
2 efg
3 kkkk(新增的行,主意Col的ID是3,也就是数据库中的值)
而我手动的代码update()后
1 abc
2 efg
6 kkkk(新增的行,主意Col的ID是6,不正确)我想要的是能让手动的代码也可以自动的把6 更新为3
dt.clear();
fill(dt);
原来是删掉重新导入的.晕来.
有关更新返回数据库自动增量值问题方法:
如:
Col(主键自动加1) Col2
1 abc
2 efg
----------------------------
1 abc
2 efg
6 kkkk(新增的行)
-------------------------------
上面可以用批查询返回自动增量值:
insert into table (col2) values('kkkk') ---------更新后col1為3
select @@Identity as col1 ---------返回自动增量值da.InsertCommand=new sqlCommand("insert into table (col2) values('kkkk');select @@Identity as col1 ");