Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build an update statement dynamically from just a Dataset

Status
Not open for further replies.

tbubbs

Programmer
Mar 30, 2001
26
CA
Hello all,
I was wondering - I have a dastaset that has been modified, is it possible to build an update/insert/delete combination of statements and NOT use a dataadapter? And then update the database using these statements that were just created, once again not using a dataadapter? I would have an unknown number of datatables and an unknown number of fields (columns). I think I'd have to loop through (for each) all of the tables and all of the fields to build these statements, but is there a way to determine what has been updated/inserted/deleted? Does anyone know how to tell when you're just given a dataset? (Or, am I just wasting my time, and if so, would it be possible to create a dataadapter on the fly??)
Any help would be much appreciated!
TBubbs.

PS:
One of the reasons why I don't want to use a dataadapter in the normal (?) way is because I'd have to set it up all of the time, all of the parameters and what not (oda.insertcommand.parameters.add(...) and so forth). While that's not hard, when you have to do it 500 times (or so) in a row, it become rather time-consuming... especially since they're all different and accessing about a total of 380 tables...
 
Well, apparently you can, though it is some work (but not much, once you get one method working well...). You have to loop through the dataset with datatable and datarow objects, and then just build your strings. You also have to have in place some rules (I suppose they would be different for every person out there, but things like primary keys must be in the first column and datatable names must match the names of the tables in the database would be rather common, I would think) for the dataset and it's structure. As you loop through, you must use the datatable objects to get the types of the columns, so you can determine the difference between 1 and "1". Oh, and the statements need to be different for each datarowstate (added, deleted - which also uses the original version of the datarow, and modified). And then you just need your connection (which I hope you already made and opened, so you don't have to open it 80 times in a row for 8 tables with 10 changes in each of them) and to execute your command.
So if anyone else needed to know, I hope this helps!
TBubbs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top