Wrox Programmer Forums
|
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 16th, 2007, 02:55 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default SqlDataAdapter

I just spent a couple hours trying to convert a command.executenonquery to SqlDataAdapter.update(ds,"tablename").

It seems the only way for SqlDataAdapter.update to work is if I query the table, add parameters, add an insertcommand, and add a new row object, then call the update method. So:

Code:
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

da.SelectCommand = new SqlCommnad("select * from table",conn);
da.Fill(ds,"table");

da.InsertCommand = new SqlCommand("insert into table (field1,field2) values (@field1,@field2)",conn);
da.InsertCommand.Parameters.Add("@field1",SqlDbType.Char,10);
da.InsertCommand.Parameters.Add("@field2",SqlDbType.Char,10);

da.InsertCommand.Parameters["@field1"].value = "field1";
da.InsertCommand.Parameters["@field2"].value = "field2";

DataRow row = ds.Tables["table"].NewRow();
row["field1"] = "field1";
row["field2"] = "field2";
ds.Tables["table"].Rows.Add(row);

da.Update(ds,"table");
Why do I need to set the values of the parameters AND make a new row object? It seems like I'm doing double work. I must be doing something wrong. In the end it works, but it bugs me that I need both to work.

Can someone chime off on this and confirm that this is indeed the way to do it or that I'm doing it wrong and how I should change my logic :)

Thanks

 
Old August 17th, 2007, 05:35 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, you need to consider using SqlCommandBuilder for your purpose, else you can try the link below which may fullfil your requirement:

http://www.codeproject.com/cs/databa...thoutsqlcb.asp

Regards
Mike

Don't expect too much, too soon.
 
Old August 17th, 2007, 04:20 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mike, I added the SqlCommandBuilder and no longer require the parameters collection.

 
Old August 20th, 2007, 04:04 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Glad to know that your problem has resolved.

Regards
Mike

Don't expect too much, too soon.





Similar Threads
Thread Thread Starter Forum Replies Last Post
sqldataadapter vgsgowrisankar C# 2005 7 April 23rd, 2008 04:32 PM
SqlDataAdapter does not have its properties thaopham215 ASP.NET 2.0 Basics 13 December 6th, 2006 05:15 PM
SqlDataAdapter Issue Tom Wing VB.NET 2002/2003 Basics 2 February 6th, 2006 01:43 PM
sqlDataAdapter [email protected] ADO.NET 1 April 8th, 2004 12:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.