473,388 Members | 1,423 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,388 software developers and data experts.

DataSet updates to dataabase

This might be a dumb question to ask. Can someone answer me with patience..
When I have a typed or un-typed dataset, the only way I can update is like
below?
Dim con As New SqlConnection(ConnectionString)

con.Open()

Dim daProductsDS As New SqlDataAdapter("Select * from ProductsDS", con)

Dim oCommandBuilder As New SqlCommandBuilder(daProductsDS)

daProductsDS.DeleteCommand = oCommandBuilder.GetDeleteCommand

daProductsDS.InsertCommand = oCommandBuilder.GetInsertCommand

daProductsDS.UpdateCommand = oCommandBuilder.GetUpdateCommand

'Apply the updates

daProductsDS.Update(dsNorthwind, "ProductsDS")
Why do I have to delete and insert? Is the old fashion way of just updating
dead in DOT.NET? Please help me understand this. Why was this done away with
in dataset ? The other way is to manually use SQL statements to do
inserts/updates. But again I lose the power of using datasets.

Vijay
Nov 20 '05 #1
3 1666
No, you do not need to delete and insert in order to update.
You can scan around for examples of how to do updates simply.
here's an example of a DataSet doing a Select from one database, and an
Update to another, using a single DataAdapter.
System.Data.SqlClient.SqlConnection c1= new
System.Data.SqlClient.SqlConnection(cstring1);
System.Data.SqlClient.SqlConnection c2= new
System.Data.SqlClient.SqlConnection(cstring2);

string strSelect="SELECT ix, Created, [Last Updated], Headline, Dept FROM
blogposts WHERE blog_ix=@blog_ix AND CONVERT(Char(10),Created,102) =
@targdate ";
string strInsert="INSERT INTO postbackups (ix, Saved, Headline, Dept)
VALUES (@ix, @Saved, @Headline, @Dept)";
System.Data.SqlClient.SqlDataAdapter da = new
System.Data.SqlClient.SqlDataAdapter();
da.SelectCommand= new System.Data.SqlClient.SqlCommand(strSelect);
da.SelectCommand.Connection= c1;

da.UpdateCommand= new System.Data.SqlClient.SqlCommand(strInsert);
da.UpdateCommand.Connection= c2;

da.SelectCommand.Parameters.Add("@blog_ix",
System.Data.SqlDbType.Int).Value= blog_ix;
da.SelectCommand.Parameters.Add("@targdate",
System.Data.SqlDbType.NVarChar).Value= targdate;
da.UpdateCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@ix", System.Data.SqlDbType.Int, 4,
"ix"));
da.UpdateCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Saved", System.Data.SqlDbType.DateTime,
8, "Created"));
da.UpdateCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Headline",
System.Data.SqlDbType.NVarChar, 50, "Headline"));
da.UpdateCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Dept", System.Data.SqlDbType.NVarChar,
50, "Dept"));

da.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "blogposts", new
System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ix", "ix"),
new System.Data.Common.DataColumnMapping("blog_ix", "blog_ix"),
new System.Data.Common.DataColumnMapping("Created", "Created"),
new System.Data.Common.DataColumnMapping("Last Updated", "Last
Updated"),
new System.Data.Common.DataColumnMapping("Headline", "Headline"),
new System.Data.Common.DataColumnMapping("Dept", "Dept")})});

System.Data.DataSet ds= new System.Data.DataSet();
da.Fill(ds, "blogposts");

// need to update the row so the DA does the insert
foreach (System.Data.DataRow r in ds.Tables[0].Rows) {
r["Created"]= System.DateTime.Now; // update the row!
System.Console.WriteLine("rowstate: " + r.RowState);
}
da.Update(ds);

"Vijay Balki" <vi****@softech-usa.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
This might be a dumb question to ask. Can someone answer me with patience.. When I have a typed or un-typed dataset, the only way I can update is like
below?
Dim con As New SqlConnection(ConnectionString)

con.Open()

Dim daProductsDS As New SqlDataAdapter("Select * from ProductsDS", con)

Dim oCommandBuilder As New SqlCommandBuilder(daProductsDS)

daProductsDS.DeleteCommand = oCommandBuilder.GetDeleteCommand

daProductsDS.InsertCommand = oCommandBuilder.GetInsertCommand

daProductsDS.UpdateCommand = oCommandBuilder.GetUpdateCommand

'Apply the updates

daProductsDS.Update(dsNorthwind, "ProductsDS")
Why do I have to delete and insert? Is the old fashion way of just updating dead in DOT.NET? Please help me understand this. Why was this done away with in dataset ? The other way is to manually use SQL statements to do
inserts/updates. But again I lose the power of using datasets.

Vijay

Nov 20 '05 #2
Vijay,
That depends on what you mean by 'update'. The DataAdapter uses the
Insert command to insert net records and the Delete to delete records as you
would expect. If you are only modifying records and not deleting or
inserting new ones you don't need these commands defined and can skip
assigning them. Note that the CommandBuilder uses optimistic concurrency
and thus the update will fail if any records are changed by another process
before you write them back.
Further questions would be more appropriate in
microsoft.public.dotnet.framework.adonet.

Ron Allen

"Vijay Balki" <vi****@softech-usa.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
This might be a dumb question to ask. Can someone answer me with patience.. When I have a typed or un-typed dataset, the only way I can update is like
below?
Dim con As New SqlConnection(ConnectionString)

con.Open()

Dim daProductsDS As New SqlDataAdapter("Select * from ProductsDS", con)

Dim oCommandBuilder As New SqlCommandBuilder(daProductsDS)

daProductsDS.DeleteCommand = oCommandBuilder.GetDeleteCommand

daProductsDS.InsertCommand = oCommandBuilder.GetInsertCommand

daProductsDS.UpdateCommand = oCommandBuilder.GetUpdateCommand

'Apply the updates

daProductsDS.Update(dsNorthwind, "ProductsDS")
Why do I have to delete and insert? Is the old fashion way of just updating dead in DOT.NET? Please help me understand this. Why was this done away with in dataset ? The other way is to manually use SQL statements to do
inserts/updates. But again I lose the power of using datasets.

Vijay

Nov 20 '05 #3
Thanks people for the replies. I got the answer from Dino's comments.
Appreciate it

Vijay

"Ron Allen" <ra****@src-us.com> wrote in message
news:uW**************@TK2MSFTNGP09.phx.gbl...
Vijay,
That depends on what you mean by 'update'. The DataAdapter uses the
Insert command to insert net records and the Delete to delete records as you would expect. If you are only modifying records and not deleting or
inserting new ones you don't need these commands defined and can skip
assigning them. Note that the CommandBuilder uses optimistic concurrency
and thus the update will fail if any records are changed by another process before you write them back.
Further questions would be more appropriate in
microsoft.public.dotnet.framework.adonet.

Ron Allen

"Vijay Balki" <vi****@softech-usa.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
This might be a dumb question to ask. Can someone answer me with

patience..
When I have a typed or un-typed dataset, the only way I can update is like below?
Dim con As New SqlConnection(ConnectionString)

con.Open()

Dim daProductsDS As New SqlDataAdapter("Select * from ProductsDS", con)

Dim oCommandBuilder As New SqlCommandBuilder(daProductsDS)

daProductsDS.DeleteCommand = oCommandBuilder.GetDeleteCommand

daProductsDS.InsertCommand = oCommandBuilder.GetInsertCommand

daProductsDS.UpdateCommand = oCommandBuilder.GetUpdateCommand

'Apply the updates

daProductsDS.Update(dsNorthwind, "ProductsDS")
Why do I have to delete and insert? Is the old fashion way of just

updating
dead in DOT.NET? Please help me understand this. Why was this done away

with
in dataset ? The other way is to manually use SQL statements to do
inserts/updates. But again I lose the power of using datasets.

Vijay


Nov 20 '05 #4

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

Similar topics

3
by: Vijay Balki | last post by:
This might be a dumb question to ask. Can someone answer me with patience.. When I have a typed or un-typed dataset, the only way I can update is like below? Dim con As New...
1
by: PV | last post by:
When performing a parent-child table updates using DataAdapters, the updates have to be performed in at least two passes. The deletes must be done child first, then parent. Conversely, the...
4
by: Frnak McKenney | last post by:
I'm using an in-core DataSet as an image of my application's 'database' (a multi-table Access97 mdb file). Updates are made to the DataTables within the DataSet via forms with bound TextBoxes,...
4
by: suzy | last post by:
hello. how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't...
0
by: JJ | last post by:
Hi, I have a DataSet that has no records in it from Database. I use this for just collecting records that I have been working on. I have a new worker dataset that goes out to database and gets a...
4
by: Kenny | last post by:
Hello all, I am wondering about the memory arrangement in C#. If I am going to load, say, 20000 data rows from about 20 tables at once, where will be the data stored in my machine? All save on...
4
by: paul | last post by:
Hi, Im trying to add a dataset to the app_code directory using vs2005 \ SQL2005 beta 2. When I run through the wizard I select the option to auto create new Stored Procedures (Select, update,...
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...
7
by: Jean Christophe Avard | last post by:
Hi! I have a dataset that retreive all the item information from the database. I need to be able to edit them, in the dataset and in the database. I have this code, could anyone tell me if I'm...
2
by: palgre | last post by:
Hi ALL, I am working in an windows based application using SQL Server 2000 as database. There are few tables (refer parent tables) in the application which are uploaded by a seprate application....
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: 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...
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
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.