2013-05-10 177 views
0

我想連接到我的NorthwindDataSet,當用戶單擊按鈕時,我希望將數據保存回數據庫。當我運行下面的代碼時,我沒有得到任何錯誤,但數據沒有保存。我想我正確連接到DataSet,我不知道爲什麼這不會挽回。更改沒有保存回數據庫

NorthwindDataSetTableAdapters.CustomersTableAdapter north = new NorthwindDataSetTableAdapters.CustomersTableAdapter(); 
NorthwindDataSet.CustomersDataTable northtable = north.GetData(); 

NorthwindDataSet northwindDataSet1 = new NorthwindDataSet(); 
NorthwindDataSet.CustomersRow newCustomersRow = 
northwindDataSet1.Customers.NewCustomersRow(); 

newCustomersRow.CustomerID = "5"; 
newCustomersRow.CompanyName = "Alfreds Futterkiste"; 

northwindDataSet1.Customers.Rows.Add(newCustomersRow); 

northwindDataSet1.Customers.AcceptChanges(); 
+0

我已經使用過這些東西了,但DataSet和DataAdapter之間的連接是什麼?我期望CustomersTableAdapter實例通過Update()或自定義方法進行更新。 – 2013-05-10 11:48:47

+0

您是否需要在TableAdapter上調用Update方法? – 2013-05-10 11:48:58

回答

2

您不僅需要通過AcceptChanges方法提交更改,還需要在表適配器上使用Update方法。

在你的情況下,它看起來就像這樣:

north.update(northwindDataSet1.Customers); 
northwindDataSet1.Customers.AcceptChanges(); 

接受更改不會提交數據到數據庫。更新確實。

+0

它看起來像你正試圖設置主鍵,所以默認情況下,框架阻止你從此(因爲PK應該是自動生成的)。我想你將不得不改變INSERT查詢和參數。當然,考慮到在DB中沒有定義標識字段,這又會阻止你在數據庫級更新PK列。 – 2013-05-10 12:05:36

相關問題