UPDATING Dataset Fails?

≯℡__Kan透↙ 提交于 2019-12-11 16:21:52

问题


According to this article : http://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx , after I insert/change data in the dataset, I need to call the UPDATE to synchronize my database data with the dataset data.

My problem is this:

I have created a dataset called dataset1.xsd and then I create a new TableAdapter to do my INSERT query with, and THEN I need to somehow let my database know that the stuff in dataset has changed.

DataSet1TableAdapters.reservationsTableAdapter ta = new DataSet1TableAdapters.reservationsTableAdapter();

ta.Insert(LastName,Arrival,Departure);  // this is where I do the INSERT query

Now I should update the dataset, right? How do I do this? The article I posted above suggests doing something like this:

ta.Update(DataSet1.reservationsDataTAble);

However, I can't do this because:

Error - 'myproject.DataSet1.reservationsDataTable' is a 'type', which is not valid in the given context.

I tried declaring a new Dataset1 DataTable, and then updating that, but it still won't show any changes in my database.

I KNOW, however, that the changes are saved in the DataSet, because when I fill a new datatable later, the record is there.

EDIT: Thanks to the comments, below, I tried doing this for a change:

DataSet1.reservationsDataTable NDT = new DataSet1.reservationsDataTable();                   

DataSet1TableAdapters.reservationsTableAdapter ta = new DataSet1TableAdapters.reservationsTableAdapter();

ta.Insert(LastName,Arrival,Departure);

ta.Fill(NDT);   
ta.Update(NDT);

... and I could see (in the debugger) that the NDT datatable DID in fact contain the data that was "INSERTED" and then filled into the data table.

However, the ta.Update(NDT); still did not update my database...


回答1:


I assume that However, the ta.Update(NDT); still did not update my database means that no sql-insert is executed and that yo get no exception.

does this work for you?

var myDataSet1 = new DataSet1();

var newReservation = myDataSet1.reservations.NewRow();

newReservation.LastName=...
newReservation.Arrival=...
newReservation.Departure=...

myDataSet1.reservations.AddreservationsRow(newReservation);

var ta = new DataSet1TableAdapters.reservationsTableAdapter();

ta.Update(myDataSet1.reservations);


来源:https://stackoverflow.com/questions/17428113/updating-dataset-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!