Updating DataSource
-
I have a dataset,and dataadapter.I add a row to dataset like this:
DataRow row = dataSet11.Tables\["Table2"\].NewRow(); row\["Username"\] = str1; row\["Password"\] = str2; row\["ID"\] = 65; dataSet11.Tables\["Table2"\].Rows.Add(row);
Now I want to make this change to datasource,so I used this: oleDbDataAdapter1.Update(dataSet11,"Table2"); But unhandled error thrown for this line,and it said no valid update command. I looked into some MSDN doc and there were talking about modifying recordsets in dataset not add new records,now how can I reflect changes and additions from dataset to datasource?Does
Update()
only for midifying or its for adding records too? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
Wish You Were Here-Pink Floyd-1975 -
I have a dataset,and dataadapter.I add a row to dataset like this:
DataRow row = dataSet11.Tables\["Table2"\].NewRow(); row\["Username"\] = str1; row\["Password"\] = str2; row\["ID"\] = 65; dataSet11.Tables\["Table2"\].Rows.Add(row);
Now I want to make this change to datasource,so I used this: oleDbDataAdapter1.Update(dataSet11,"Table2"); But unhandled error thrown for this line,and it said no valid update command. I looked into some MSDN doc and there were talking about modifying recordsets in dataset not add new records,now how can I reflect changes and additions from dataset to datasource?Does
Update()
only for midifying or its for adding records too? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
Wish You Were Here-Pink Floyd-1975If you're adding a new Row and then calling Update on the DataAdapter, you should assign something to the InsertCommand Property of DataAdapter. InsertCommand takes a SQL Sentence (string) or OleDbCommand. Or the alternative using OleDbCommandBuilder. This class generates the SQL Statements. See: InsertCommand DeleteCommand SelectCommand OleDbCommand OleDbCommandBuilder Andres Manggini. Buenos Aires - Argentina.
-
If you're adding a new Row and then calling Update on the DataAdapter, you should assign something to the InsertCommand Property of DataAdapter. InsertCommand takes a SQL Sentence (string) or OleDbCommand. Or the alternative using OleDbCommandBuilder. This class generates the SQL Statements. See: InsertCommand DeleteCommand SelectCommand OleDbCommand OleDbCommandBuilder Andres Manggini. Buenos Aires - Argentina.
Thanks Andres,I got the point, Now if I want to add 10 Records to DataSet and want to update to DataAdapter,Should I use 10 InsertCommand ,Or is there anyway to put all of them in one InsertCommand? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
Wish You Were Here-Pink Floyd-1975 -
Thanks Andres,I got the point, Now if I want to add 10 Records to DataSet and want to update to DataAdapter,Should I use 10 InsertCommand ,Or is there anyway to put all of them in one InsertCommand? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
Wish You Were Here-Pink Floyd-1975The same InsertCommand will be call as many times as records were added. That's the beauty of it, you can insert a few records, delete others and make a couple of changes on others, then set the three commands (InsertCommand, DeleteCommand, UpdateCommand) and let the DataAdapter do all the updates to the Database. Andres Manggini. Buenos Aires - Argentina.
-
The same InsertCommand will be call as many times as records were added. That's the beauty of it, you can insert a few records, delete others and make a couple of changes on others, then set the three commands (InsertCommand, DeleteCommand, UpdateCommand) and let the DataAdapter do all the updates to the Database. Andres Manggini. Buenos Aires - Argentina.
hmmm,I got it.But there is another question for me here: Each time I make a change to dataset,I have to update DataAdapter,so for large database I don't think its good. Or I can update DataAdapter after all changes.What is a solution for that too? Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
Wish You Were Here-Pink Floyd-1975 -
hmmm,I got it.But there is another question for me here: Each time I make a change to dataset,I have to update DataAdapter,so for large database I don't think its good. Or I can update DataAdapter after all changes.What is a solution for that too? Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
Wish You Were Here-Pink Floyd-1975The idea is you make all the changes needed on the DataSet, once you're done and want to commit these changes to the database you call Update on the DataAdapter, and the changes will be reflected on the Database. There is no need (of course this is depending on your design) to save the dataset after each change, it works sort of a batch process. Hope that clarify a bit. If you have doubts just ask again :) Andres Manggini. Buenos Aires - Argentina.
-
The idea is you make all the changes needed on the DataSet, once you're done and want to commit these changes to the database you call Update on the DataAdapter, and the changes will be reflected on the Database. There is no need (of course this is depending on your design) to save the dataset after each change, it works sort of a batch process. Hope that clarify a bit. If you have doubts just ask again :) Andres Manggini. Buenos Aires - Argentina.
One more thing.. If you're dealing with large DataSet's, there is this method: GetChanges. Doc: Gets a copy of the DataSet containing all changes made to it since it was last loaded It can be very useful in distributed scenarios. Andres Manggini. Buenos Aires - Argentina.
-
One more thing.. If you're dealing with large DataSet's, there is this method: GetChanges. Doc: Gets a copy of the DataSet containing all changes made to it since it was last loaded It can be very useful in distributed scenarios. Andres Manggini. Buenos Aires - Argentina.