473,387 Members | 1,529 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.

Update more than 1 table in a DataSet using SqlDataAdapter.Update

LP
Hello,

In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to
apply changes in a DataTable back to its table source in SQL Server. It
worked fine when DataSet had only 1 table, this time I have 2 tables in a
dataset and I want to update them all at once. However it only updates the
first one and fails when attempts to update 2nd table.. Has anyone done
anything like that? It looks like SqlCommandBuilder generates insert command
only for the first table.

Here's the most relevant code, if it helps:

//command that gets 2 tables structure
SqlCommand commandClosedEnd = new SqlCommand("select f1, f2 from table1
where 1=2\r\nselect f1, f2 from table2 where 1=2", connClosedEnd);

commandClosedEnd.CommandType = CommandType.Text;
DataSet dsClosedEnd = new DataSet();
SqlDataAdapter daClosedEnd = new SqlDataAdapter();
daClosedEnd.SelectCommand = commandClosedEnd;//select command

daClosedEnd.Fill(dsClosedEnd); //gets 2 tables

/*
Code inserts new rows to both tables
*/

//Generate insert command
SqlCommandBuilder cmndBuild = new SqlCommandBuilder(daClosedEnd);
daClosedEnd.InsertCommand = cmndBuild.GetInsertCommand(); //should
genereate for 2 tables, but it doesn't
//update all 2 tables -- updates the first 1 and breaks on the second
foreach (DataTable tblUpdate in dsClosedEnd.Tables)
{
daClosedEnd.Update(dsClosedEnd);
}

Please help!!!!
Nov 21 '05 #1
3 6887
Hi,

http://msdn.microsoft.com/library/de...andbuilder.asp

Ken
-----------------------------

"LP" wrote:
Hello,

In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to
apply changes in a DataTable back to its table source in SQL Server. It
worked fine when DataSet had only 1 table, this time I have 2 tables in a
dataset and I want to update them all at once. However it only updates the
first one and fails when attempts to update 2nd table.. Has anyone done
anything like that? It looks like SqlCommandBuilder generates insert command
only for the first table.

Here's the most relevant code, if it helps:

//command that gets 2 tables structure
SqlCommand commandClosedEnd = new SqlCommand("select f1, f2 from table1
where 1=2\r\nselect f1, f2 from table2 where 1=2", connClosedEnd);

commandClosedEnd.CommandType = CommandType.Text;
DataSet dsClosedEnd = new DataSet();
SqlDataAdapter daClosedEnd = new SqlDataAdapter();
daClosedEnd.SelectCommand = commandClosedEnd;//select command

daClosedEnd.Fill(dsClosedEnd); //gets 2 tables

/*
Code inserts new rows to both tables
*/

//Generate insert command
SqlCommandBuilder cmndBuild = new SqlCommandBuilder(daClosedEnd);
daClosedEnd.InsertCommand = cmndBuild.GetInsertCommand(); //should
genereate for 2 tables, but it doesn't
//update all 2 tables -- updates the first 1 and breaks on the second
foreach (DataTable tblUpdate in dsClosedEnd.Tables)
{
daClosedEnd.Update(dsClosedEnd);
}

Please help!!!!

Nov 21 '05 #2
LP
Hi,
Thanks for the article it doesn't help me though. It reinforces my concern
about CommandBuilder limitations:

a.. The SelectCommand cannot refer to SQL queries, stored procedures, or
views that contain JOIN operators. This means your SELECT statement must
refer to a single table.

So, I think the answer is I can only update one table at a time or write my
own custom update command logic.
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
Hi,

http://msdn.microsoft.com/library/de...andbuilder.asp
Ken
-----------------------------

"LP" wrote:
Hello,

In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to apply changes in a DataTable back to its table source in SQL Server. It
worked fine when DataSet had only 1 table, this time I have 2 tables in a dataset and I want to update them all at once. However it only updates the first one and fails when attempts to update 2nd table.. Has anyone done
anything like that? It looks like SqlCommandBuilder generates insert command only for the first table.

Here's the most relevant code, if it helps:

//command that gets 2 tables structure
SqlCommand commandClosedEnd = new SqlCommand("select f1, f2 from table1
where 1=2\r\nselect f1, f2 from table2 where 1=2", connClosedEnd);

commandClosedEnd.CommandType = CommandType.Text;
DataSet dsClosedEnd = new DataSet();
SqlDataAdapter daClosedEnd = new SqlDataAdapter();
daClosedEnd.SelectCommand = commandClosedEnd;//select command

daClosedEnd.Fill(dsClosedEnd); //gets 2 tables

/*
Code inserts new rows to both tables
*/

//Generate insert command
SqlCommandBuilder cmndBuild = new SqlCommandBuilder(daClosedEnd);
daClosedEnd.InsertCommand = cmndBuild.GetInsertCommand(); //should
genereate for 2 tables, but it doesn't
//update all 2 tables -- updates the first 1 and breaks on the second
foreach (DataTable tblUpdate in dsClosedEnd.Tables)
{
daClosedEnd.Update(dsClosedEnd);
}

Please help!!!!

Nov 21 '05 #3
The CommandBuilder object is a quick and dirty way to get something done.
Even for the simplistic case you speak of, the SQL query it generates is
awfully ugly and inefficient. You are better off implementing your own
queries logic IMO.
--

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/

..

"LP" <lp@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
Thanks for the article it doesn't help me though. It reinforces my concern
about CommandBuilder limitations:

a.. The SelectCommand cannot refer to SQL queries, stored procedures, or
views that contain JOIN operators. This means your SELECT statement must
refer to a single table.

So, I think the answer is I can only update one table at a time or write
my
own custom update command logic.
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> wrote in
message
news:6A**********************************@microsof t.com...
Hi,

http://msdn.microsoft.com/library/de...andbuilder.asp

Ken
-----------------------------

"LP" wrote:
> Hello,
>
> In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to > apply changes in a DataTable back to its table source in SQL Server. It
> worked fine when DataSet had only 1 table, this time I have 2 tables in a > dataset and I want to update them all at once. However it only updates the > first one and fails when attempts to update 2nd table.. Has anyone
> done
> anything like that? It looks like SqlCommandBuilder generates insert command > only for the first table.
>
> Here's the most relevant code, if it helps:
>
> //command that gets 2 tables structure
> SqlCommand commandClosedEnd = new SqlCommand("select f1, f2 from table1
> where 1=2\r\nselect f1, f2 from table2 where 1=2", connClosedEnd);
>
> commandClosedEnd.CommandType = CommandType.Text;
> DataSet dsClosedEnd = new DataSet();
> SqlDataAdapter daClosedEnd = new SqlDataAdapter();
> daClosedEnd.SelectCommand = commandClosedEnd;//select command
>
> daClosedEnd.Fill(dsClosedEnd); //gets 2 tables
>
> /*
> Code inserts new rows to both tables
> */
>
> //Generate insert command
> SqlCommandBuilder cmndBuild = new SqlCommandBuilder(daClosedEnd);
> daClosedEnd.InsertCommand = cmndBuild.GetInsertCommand(); //should
> genereate for 2 tables, but it doesn't
> //update all 2 tables -- updates the first 1 and breaks on the second
> foreach (DataTable tblUpdate in dsClosedEnd.Tables)
> {
> daClosedEnd.Update(dsClosedEnd);
> }
>
> Please help!!!!
>
>
>


Nov 21 '05 #4

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

Similar topics

3
by: Fernando | last post by:
I am having problems with updating the changes done over a DataSet to a Database. All the changes being done using the Form are stored in the Dataset, but when i decide to close the Form and open...
3
by: LP | last post by:
Hello, In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to apply changes in a DataTable back to its table source in SQL Server. It worked fine when DataSet had only 1...
1
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The...
4
by: Oscar Thornell | last post by:
Hi, I have a relativley large/complex typed dataset that contains 7-8 tables and some supporting relational tables (lookups) for many-to-many relations. A good exampel would be a dataset that...
4
by: CaptRR | last post by:
I think this is the right group to post to, so here goes. My problem is this, I cannot update the datarow to save my life. Been on this for 2 days now, and still am no closer to figuring it out...
4
by: steroche | last post by:
I would REALLY appreciate help please please please! Im sure it is probably blindingly obvious to most of you but I am totally in the dark here!I am lost - i thought i had finally figured out this...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
3
by: RSH | last post by:
Hi, I have a situation in where i have two instances of SQL server, the first is our Production Environment, the second is our Development environment. Both servers contain the same databases...
1
by: TonyJ | last post by:
Hello! I trying to update a table using sql server but no update will be done. I don't get any error at all. Here is an extract from the code. Does this seems to be right.
0
by: mcasey0827 | last post by:
I'm writing a little app to get data from a CSV file generated everyday by a vendor and insert it into a Table in our SQL Server database. I can get the data fine, and I can easily merge it with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.