我遇到一个不是问题的问题。
下面这段代码,从头到尾不报错,但是就是更新不到数据库。
我实在不知道哪有问题,还是少了什么,真的已经快崩溃了。
到底哪写的不对。各位帮忙。
1 /// <summary>
2 /// 利用两个dataset合并更新数据库表
3 /// </summary>
4 /// <param name="ds">新数据的dataset</param>
5 public void update(DataSet ds)
6 {
7
8
9 string CompanyInfoColumn = "Info_Code,Company_Name,Company_Address,ZIP,Country,Tel,Fax,Email,Web,Vocation,";
10 CompanyInfoColumn += "Company_Business,Business_Type,Products_Service,Employee_Number,SPECIAL,OTHER,PRODUCTBARCODE";
11
12 string CmdText="Select "+CompanyInfoColumn+" From CompanyInfo";
13
14 DataSet Ods = Acc.getData(CmdText);//或数据库获得已经有的数据
15
16 Ods.Tables[0].Merge(ds.Tables[0], false);//合并两个dataset
17
18
19
20
21 //以下设置OleDbDataAdapter以更新Ods
22 OleDbDataAdapter oda = new OleDbDataAdapter();
23
24 OleDbCommand odcsel = new OleDbCommand();
25 odcsel.Connection = Acc.OleConn;//Acc.OleConn是一个连接
26 odcsel.CommandText = CompanyInfoColumn;
27 oda.SelectCommand = odcsel;
28
29 OleDbCommand odcdel = new OleDbCommand();
30 odcdel.Connection = Acc.OleConn;
31 odcdel.CommandText = "DELETE FROM [CompanyInfo] WHERE [Info_Code] = ?";
32 oda.DeleteCommand = odcdel;
33
34 OleDbCommand odcIns = new OleDbCommand();
35 odcIns.Connection = Acc.OleConn;
36 odcIns.CommandText = "INSERT INTO [CompanyInfo] ( [Info_Code], [Company_Name], [Company_Address], [ZIP], [Country], [Tel], [Fax], [Email], [Web], [Vocation], [Company_Business], [Business_Type], [Products_Service], [Employee_Number], [Company_Flag], [SPECIAL], [OTHER], [PRODUCTBARCODE]) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
37 oda.InsertCommand = odcIns;
38
39 OleDbCommand odcupd = new OleDbCommand();
40 odcupd.Connection = Acc.OleConn;
41 odcupd.CommandText = "UPDATE [CompanyInfo] SET [Company_Name] = ?, [Company_Address] = ?, [ZIP] = ?, [Country] = ?, [Tel] = ?, [Fax] = ?, [Email] = ?, [Web] = ?, [Vocation] = ?, [Company_Business] = ?, [Business_Type] = ?, [Products_Service] = ?, [Employee_Number] = ?, [Company_Flag] = ?, [SPECIAL] = ?, [OTHER] = ?, [PRODUCTBARCODE] = ? WHERE [Info_Code] = ?";
42 oda.UpdateCommand = odcupd;
43
44
45 //更新到表
46 oda.Update(Ods.Tables[0]);
47
48 Ods.AcceptChanges();
49
2 /// 利用两个dataset合并更新数据库表
3 /// </summary>
4 /// <param name="ds">新数据的dataset</param>
5 public void update(DataSet ds)
6 {
7
8
9 string CompanyInfoColumn = "Info_Code,Company_Name,Company_Address,ZIP,Country,Tel,Fax,Email,Web,Vocation,";
10 CompanyInfoColumn += "Company_Business,Business_Type,Products_Service,Employee_Number,SPECIAL,OTHER,PRODUCTBARCODE";
11
12 string CmdText="Select "+CompanyInfoColumn+" From CompanyInfo";
13
14 DataSet Ods = Acc.getData(CmdText);//或数据库获得已经有的数据
15
16 Ods.Tables[0].Merge(ds.Tables[0], false);//合并两个dataset
17
18
19
20
21 //以下设置OleDbDataAdapter以更新Ods
22 OleDbDataAdapter oda = new OleDbDataAdapter();
23
24 OleDbCommand odcsel = new OleDbCommand();
25 odcsel.Connection = Acc.OleConn;//Acc.OleConn是一个连接
26 odcsel.CommandText = CompanyInfoColumn;
27 oda.SelectCommand = odcsel;
28
29 OleDbCommand odcdel = new OleDbCommand();
30 odcdel.Connection = Acc.OleConn;
31 odcdel.CommandText = "DELETE FROM [CompanyInfo] WHERE [Info_Code] = ?";
32 oda.DeleteCommand = odcdel;
33
34 OleDbCommand odcIns = new OleDbCommand();
35 odcIns.Connection = Acc.OleConn;
36 odcIns.CommandText = "INSERT INTO [CompanyInfo] ( [Info_Code], [Company_Name], [Company_Address], [ZIP], [Country], [Tel], [Fax], [Email], [Web], [Vocation], [Company_Business], [Business_Type], [Products_Service], [Employee_Number], [Company_Flag], [SPECIAL], [OTHER], [PRODUCTBARCODE]) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
37 oda.InsertCommand = odcIns;
38
39 OleDbCommand odcupd = new OleDbCommand();
40 odcupd.Connection = Acc.OleConn;
41 odcupd.CommandText = "UPDATE [CompanyInfo] SET [Company_Name] = ?, [Company_Address] = ?, [ZIP] = ?, [Country] = ?, [Tel] = ?, [Fax] = ?, [Email] = ?, [Web] = ?, [Vocation] = ?, [Company_Business] = ?, [Business_Type] = ?, [Products_Service] = ?, [Employee_Number] = ?, [Company_Flag] = ?, [SPECIAL] = ?, [OTHER] = ?, [PRODUCTBARCODE] = ? WHERE [Info_Code] = ?";
42 oda.UpdateCommand = odcupd;
43
44
45 //更新到表
46 oda.Update(Ods.Tables[0]);
47
48 Ods.AcceptChanges();
49
我根据网上的资料改了一下以后代码如下,还是更新不进去。
1 string CompanyInfoColumn = "Info_Code,Company_Name,Company_Address,ZIP,Country,Tel,Fax,Email,Web,Vocation,";
2 CompanyInfoColumn += "Company_Business,Business_Type,Products_Service,Employee_Number,SPECIAL,OTHER,PRODUCTBARCODE";
3
4 string CmdText="Select "+CompanyInfoColumn+" From CompanyInfo";
5
6 DataSet Ods = Acc.getData(CmdText);//或数据库获得已经有的数据
7 DataColumn[] dc=new DataColumn[1];
8 dc[0] = new DataColumn("Info_Code");
9 Ods.Tables[0].PrimaryKey=dc;
10 Ods.Tables[0].Merge(ds.Tables[0], false);//合并两个dataset
11
12
13 OleDbDataAdapter oleda = Acc.OleDataAdapter;
14
15
16
17
18 OleDbCommandBuilder oCbd = new OleDbCommandBuilder(oleda);
19
20
21
22
23 //更新到表
24 oleda.Update(Ods.Tables[0]);
25
26 Ods.AcceptChanges();
2 CompanyInfoColumn += "Company_Business,Business_Type,Products_Service,Employee_Number,SPECIAL,OTHER,PRODUCTBARCODE";
3
4 string CmdText="Select "+CompanyInfoColumn+" From CompanyInfo";
5
6 DataSet Ods = Acc.getData(CmdText);//或数据库获得已经有的数据
7 DataColumn[] dc=new DataColumn[1];
8 dc[0] = new DataColumn("Info_Code");
9 Ods.Tables[0].PrimaryKey=dc;
10 Ods.Tables[0].Merge(ds.Tables[0], false);//合并两个dataset
11
12
13 OleDbDataAdapter oleda = Acc.OleDataAdapter;
14
15
16
17
18 OleDbCommandBuilder oCbd = new OleDbCommandBuilder(oleda);
19
20
21
22
23 //更新到表
24 oleda.Update(Ods.Tables[0]);
25
26 Ods.AcceptChanges();