Question : How to commit changes of a Typed Dataset to database at once

Hi,

        I am using Dotnet C# 2.0, also i am using a typed dataset. The scinario is not straight forward. Actually the typed dataset is filled with data from a xml file. While initially typed dataset is not connected to database and is also blank. After reading xml file now typed dataset is having data in parent and child tables.

       The problem is to update the data from typed dataset to sql server database again. I am not finding any method nor any reference to perform this operation. Please help.

Thanks and
Warm Regards

Pradip

Answer : How to commit changes of a Typed Dataset to database at once



I did some research over the web. Below are my understanding.

To update dataset to the database the typical method is to use SqlDataAdapter. But this requires to pass sql commands as well, at least SELECT statement with SqlCommandBuilder. Again SqlCommandBuilder is meant only for single-table updates. The example in this msdn link shows that => http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx

To update multi-tables, using the same SqlDataAdapter/SqlCommandBuilder, it should be updated table-by-table one adapter for each table. This link shows that => http://msdn.microsoft.com/en-us/library/4esb49b4(v=VS.80).aspx

If you have only few tables to update, then I would say to follow the methods above. Since the typed dataset has been created from the database its schema would match the database.

Alternatively you can directly import the xml data into database using SQL Server's BULK INSERT or OPENROWSET(BULKā€¦) or bcp utility. Again the exampless I have seen so far deals with one table at a time. => http://msdn.microsoft.com/en-us/library/ms175915.aspx

or if there is a third-party code library that scans the dataset and creates sqls and does the update, then that's what you are asking for. Basically 'update dataset to database without using sqldataadapter'. I will check and update if i find any such code.

or you can use other tools to export xml data to database like Altova DatabaseSpy, XMLSpy =>http://www.altova.com/databasespy/database-import-export.html

For the basic single table update below code works for me:

DataSet dataSet;
// dataset is filled here

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand("SELECT col1, col2 FROM Table1", connection);
        SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

        connection.Open();

        builder.GetUpdateCommand();
       
        adapter.Update(dataSet, tableName);
    }

Regarding your notes on dataset and connection, dataset doesnot have or store connection details. that is its special feature (dis-connected nature). It is the SqlDataAdapter that uses the connection. Also I think the problem is the same whether it is dataset or typed dataset. Typed dataset just helps you to use it like a business object. Underlying commit taks is the same for both.

Hope this helps to show you some light.








 
Random Solutions  
 
programming4us programming4us