C# DataSet Update to Database

Hi, I'm new with .Net and have a question about updating DataSets back to the database.  I have a basic sample as follows:

SqlDataAdapter myDA = new SqlDataAdapter("Select * from Table_Test", myConn);
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(myDA);
myDA.Update(ds, "Test");

The above works, but I was wondering what happens if I have multiple DataSets created at different times in an application, how would I know the the Select statement or command used to assign to the Data Adapter each time i want to update a DataSet back to the database?  Would I have to track the command used to build each DataSet or is there a way to build/parse that command from the individual DataSets through it's properties.

Thanks!
sonnytleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

apeterCommented:
In order to run Update method of SqlDataAdapter you must have to configure InsertCommand, DeleteCommand and UpdateCommand properties along with SelectCommand of SqlDataAdapter or construct the SqlCommandBuilder object which configure these commands implicitly. You don't need to track if you are using the later one.
0
sonnytleAuthor Commented:
Thank you for the response. Can you write me an example of a class where I can pass a dataset object then it will use the SqlCommandBuilder to configure the commands?
0
apeterCommented:
Hope this helps.

public void BuildCommandBuilder(DataSet ds)
{
 string tableName = dsBatches.Tables[0].TableName;

SqlDataAdapter myDA = new SqlDataAdapter(string.format("Select * from {0}", tableName ), myConn);
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(myDA);
myDA.Update(ds, "Test");

}
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

sonnytleAuthor Commented:
Sorry, that did not work.  When i fill the dataset my sql is "Select * from Table_Test".  I then named the dataset table "Test":

myDA.Fill(ds, "Test");

The ds.Tables[0].TableName returns "Test" which is not the actual table in the database which is "Table_Test".  Furthermore, what happens if my sql was more complex with joins?

Please let me know.  Your response is greatly appreciated.
0
apeterCommented:
Give the dataset table value as same as "table name" which you are using in the sql query.  

In complex scenario, maybe you should manually provide the sql commands for insert/update and delete.

Since you just starting with .net...did you had a chance to look into "Entity Framework" , http://msdn.microsoft.com/en-gb/data/ef.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sonnytleAuthor Commented:
So if i have many dataset in an app, i would need to track each sql commands for each in order to update the dataset later?  There's no generic way to just pass any dataset to one class to update?

In VB 6.0, i was able to do so, therefore I was hoping to do the same in .NET.  Here's the sample code in VB 6.0:

Public Function Update (ByVal objRecordset As ADODB.Recordset) As ADODB.Recordset
 
    Set Update = objRecordset
    objRecordset.MarshalOptions = adMarshalModifiedOnly
   
    objRecordset.ActiveConnection = "Provider=SQLOLEDB;Server=      <servername>;Database=<dbname>;UID=<>;PWD=<>;Network=dbmssocn;"
   
  objRecordset.UpdateBatch
   

    Set objRecordset.ActiveConnection = Nothing
    Set objRecordset = Nothing

End Function
0
sonnytleAuthor Commented:
His input helped me build my data class to do what i needed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.