更新數據集後嘗試更新我的數據庫,但即使調用數據適配器的更新函數,底層數據庫也不會更改。我使用SqlCommandBuilder,它仍然不會更新。一旦我打開GUI,我可以提交和搜索數據集中的值,但是一旦關閉它然後重新運行它,所有更改都將消失。幫幫我?通過C#/ Visual Studio中的DataAdapters/datasets更新數據庫的問題
Code:
//in my form load event
con = new System.Data.SqlClient.SqlConnection();
ds1 = new DataSet();
ds2 = new DataSet();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";
con.Open();
MessageBox.Show("Database Open.");
string sql = "SELECT * From tblPurchases";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
string cust = "SELECT * From tblCustomers";
da2 = new System.Data.SqlClient.SqlDataAdapter(cust, con);
da.Fill(ds1, "Purchases");
numOfRecords = ds1.Tables["Purchases"].Rows.Count;
da2.Fill(ds2, "Customers");
numOfCustomers = ds2.Tables["Customers"].Rows.Count;
con.Close();
MessageBox.Show("Database Closed.");
Code:
//in my submit button click event
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
System.Data.SqlClient.SqlCommandBuilder cb2;
cb2 = new System.Data.SqlClient.SqlCommandBuilder(da2);
DataRow dRow = ds1.Tables["Purchases"].NewRow();
DataRow dRow2 = ds2.Tables["Customers"].NewRow();
//dRow updating happens here
ds1.Tables["Purchases"].Rows.Add(dRow);
ds2.Tables["Customers"].Rows.Add(dRow2);
da.Update(ds1, "Purchases");
da2.Update(ds2, "Customers");
就是這樣。沒有錯誤消息,只是即使這樣,底層數據庫也不會更新,更新只能達到數據集。
歡迎來到StackOverflow!編輯您的問題以包含相關代碼和實際錯誤消息以幫助說明。 – 2010-08-07 05:56:32
我已更新相關代碼的帖子。 – Icyie 2010-08-07 06:19:21