473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to map all changes of a dataset to the original database?

It seems that a C# program can not update the original database by mapping all the changes to the dataset, but have to explicitly assign the InsertCommand or UpdateCommand properties of a SqlDataAdapter instance. Look at the following codes about inserting a few records into the country table

SqlConnection myConn = new SqlConnection("server=(local)\\DANIEL;Trusted_Conn ection=yes;database=daniel1")
SqlDataAdapter myDataAdapter = new SqlDataAdapter()

// Create the SelectCommand
SqlCommand cmd = new SqlCommand("SELECT * FROM Country where name = 'Spain'", myConn)

myDataAdapter.SelectCommand = cmd

myConn.Open()
DataSet custDS = new DataSet()
myDataAdapter.Fill(custDS, "country")

// Create the InsertCommand
cmd = new SqlCommand("INSERT INTO Country " +
"VALUES(@Name, @Area);", myConn)
cmd.Parameters.Add("@Name", SqlDbType.Char, 20).Value = "India"
cmd.Parameters.Add("@Area", SqlDbType.Int).Value = 2970000
myDataAdapter.InsertCommand = cmd

DataRow myRow
custDS.Tables["country"].Clear()
myRow = custDS.Tables["country"].NewRow()
custDS.Tables["country"].Rows.Add(myRow)

myDataAdapter.Update(custDS, "country")

// Create the InsertCommand
cmd = new SqlCommand("INSERT INTO Country " +
"VALUES(@Name, @Area);", myConn)
cmd.Parameters.Add("@Name", SqlDbType.Char, 20).Value = "Sweden"
cmd.Parameters.Add("@Area", SqlDbType.Int).Value = 440000
myDataAdapter.InsertCommand = cmd

myRow = custDS.Tables["country"].NewRow()

myDataAdapter.Update(custDS, "country")
myConn.Close()

Pay attention to those assignment statements of InsertCommand. Could we try to discard them by just modifying the dataset and then updating the original database by mapping the dataset back

Nov 16 '05 #1
1 1529
You can use commandbuilder and skip creating the insertstatement

Taken from MSDN
(ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfSystemDataSqlClientSqlCom
mandBuilderClassTopic.htm):

string myConnection =
"server=(local)\\DANIEL;Trusted_Connection=yes;dat abase=daniel1";
string mySelectQuery = "SELECT * FROM Country where name = 'Spain'";
string myTableName = "country";
SqlConnection myConn = new SqlConnection(myConnection);
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

myConn.Open();

DataSet ds = new DataSet();
myDataAdapter.Fill(ds, myTableName);

//code to modify data in DataSet here

//Without the SqlCommandBuilder this line would fail
myDataAdapter.Update(ds, myTableName);

myConn.Close();

this will update your database directly

Regards
Martin

"moonriver" <xi*******@yahoo.com> wrote in message
news:4E**********************************@microsof t.com...
It seems that a C# program can not update the original database by mapping all the changes to the dataset, but have to explicitly assign the
InsertCommand or UpdateCommand properties of a SqlDataAdapter instance. Look
at the following codes about inserting a few records into the country table:
SqlConnection myConn = new SqlConnection("server=(local)\\DANIEL;Trusted_Conn ection=yes;database=daniel
1"); SqlDataAdapter myDataAdapter = new SqlDataAdapter();

// Create the SelectCommand.
SqlCommand cmd = new SqlCommand("SELECT * FROM Country where name = 'Spain'", myConn);
myDataAdapter.SelectCommand = cmd;

myConn.Open();
DataSet custDS = new DataSet();
myDataAdapter.Fill(custDS, "country");

// Create the InsertCommand.
cmd = new SqlCommand("INSERT INTO Country " +
"VALUES(@Name, @Area);", myConn);
cmd.Parameters.Add("@Name", SqlDbType.Char, 20).Value = "India";
cmd.Parameters.Add("@Area", SqlDbType.Int).Value = 2970000;
myDataAdapter.InsertCommand = cmd;

DataRow myRow;
custDS.Tables["country"].Clear();
myRow = custDS.Tables["country"].NewRow();
custDS.Tables["country"].Rows.Add(myRow);

myDataAdapter.Update(custDS, "country");

// Create the InsertCommand.
cmd = new SqlCommand("INSERT INTO Country " +
"VALUES(@Name, @Area);", myConn);
cmd.Parameters.Add("@Name", SqlDbType.Char, 20).Value = "Sweden";
cmd.Parameters.Add("@Area", SqlDbType.Int).Value = 440000;
myDataAdapter.InsertCommand = cmd;

myRow = custDS.Tables["country"].NewRow();

myDataAdapter.Update(custDS, "country");
myConn.Close();
Pay attention to those assignment statements of InsertCommand. Could we try to discard them by just modifying the dataset and then updating the
original database by mapping the dataset back?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Alan McIntyre | last post by:
Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my...
8
by: Bruce Stockwell | last post by:
the setup: Webservice/WinClient application/SQL server. VS.Net (visual basic) winform wizard creates a simple form with load cancel cancelall and datagrid bound to a simple Dataset with one...
40
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. ...
5
by: Grant | last post by:
Hello, How come when I add a new row to my dataset table it shows up as changed (agencyData.Haschanges() = True) but when I delete a row the dataset thinks here are no...
2
by: deko | last post by:
I create an XmlDataDocument Dataset when the main form of my WinForms app opens. The user will make changes to the Dataset, and then those changes should be saved to the XML file when the code...
4
by: Peter Proost | last post by:
Hello group, what would be the best way to do the next thing: I've got a grid form with about 15000 records, when I double click a row a detail form is opened and the user can modify and save...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
2
by: Rob Dob | last post by:
How do I determine if there are any rows within the BindingSource that have changed prior to calling the EndEdit(). I need to do this in order to archive some records and if I call the EndEdit()...
0
by: RSH | last post by:
I have a simple application that is filling a dataset, importing several rows, building an Insert Statement, then attempting to commit the changes. No exceptions are being raised but the data is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.