473,666 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C onnectionString )

con.Open()

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

Dim oCommandBuilder As New SqlCommandBuild er(daProductsDS )

daProductsDS.De leteCommand = oCommandBuilder .GetDeleteComma nd

daProductsDS.In sertCommand = oCommandBuilder .GetInsertComma nd

daProductsDS.Up dateCommand = oCommandBuilder .GetUpdateComma nd

'Apply the updates

daProductsDS.Up date(dsNorthwin d, "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
Jul 21 '05 #1
3 2704
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.Sql Client.SqlConne ction c1= new
System.Data.Sql Client.SqlConne ction(cstring1) ;
System.Data.Sql Client.SqlConne ction c2= new
System.Data.Sql Client.SqlConne ction(cstring2) ;

string strSelect="SELE CT ix, Created, [Last Updated], Headline, Dept FROM
blogposts WHERE blog_ix=@blog_i x AND CONVERT(Char(10 ),Created,102) =
@targdate ";
string strInsert="INSE RT INTO postbackups (ix, Saved, Headline, Dept)
VALUES (@ix, @Saved, @Headline, @Dept)";
System.Data.Sql Client.SqlDataA dapter da = new
System.Data.Sql Client.SqlDataA dapter();
da.SelectComman d= new System.Data.Sql Client.SqlComma nd(strSelect);
da.SelectComman d.Connection= c1;

da.UpdateComman d= new System.Data.Sql Client.SqlComma nd(strInsert);
da.UpdateComman d.Connection= c2;

da.SelectComman d.Parameters.Ad d("@blog_ix",
System.Data.Sql DbType.Int).Val ue= blog_ix;
da.SelectComman d.Parameters.Ad d("@targdate" ,
System.Data.Sql DbType.NVarChar ).Value= targdate;
da.UpdateComman d.Parameters.Ad d(new
System.Data.Sql Client.SqlParam eter("@ix", System.Data.Sql DbType.Int, 4,
"ix"));
da.UpdateComman d.Parameters.Ad d(new
System.Data.Sql Client.SqlParam eter("@Saved", System.Data.Sql DbType.DateTime ,
8, "Created")) ;
da.UpdateComman d.Parameters.Ad d(new
System.Data.Sql Client.SqlParam eter("@Headline ",
System.Data.Sql DbType.NVarChar , 50, "Headline") );
da.UpdateComman d.Parameters.Ad d(new
System.Data.Sql Client.SqlParam eter("@Dept", System.Data.Sql DbType.NVarChar ,
50, "Dept"));

da.TableMapping s.AddRange(new System.Data.Com mon.DataTableMa pping[] {
new System.Data.Com mon.DataTableMa pping("Table", "blogposts" , new
System.Data.Com mon.DataColumnM apping[] {
new System.Data.Com mon.DataColumnM apping("ix", "ix"),
new System.Data.Com mon.DataColumnM apping("blog_ix ", "blog_ix"),
new System.Data.Com mon.DataColumnM apping("Created ", "Created"),
new System.Data.Com mon.DataColumnM apping("Last Updated", "Last
Updated"),
new System.Data.Com mon.DataColumnM apping("Headlin e", "Headline") ,
new System.Data.Com mon.DataColumnM apping("Dept", "Dept")})}) ;

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

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

"Vijay Balki" <vi****@softe ch-usa.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(C onnectionString )

con.Open()

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

Dim oCommandBuilder As New SqlCommandBuild er(daProductsDS )

daProductsDS.De leteCommand = oCommandBuilder .GetDeleteComma nd

daProductsDS.In sertCommand = oCommandBuilder .GetInsertComma nd

daProductsDS.Up dateCommand = oCommandBuilder .GetUpdateComma nd

'Apply the updates

daProductsDS.Up date(dsNorthwin d, "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

Jul 21 '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.publi c.dotnet.framew ork.adonet.

Ron Allen

"Vijay Balki" <vi****@softe ch-usa.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(C onnectionString )

con.Open()

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

Dim oCommandBuilder As New SqlCommandBuild er(daProductsDS )

daProductsDS.De leteCommand = oCommandBuilder .GetDeleteComma nd

daProductsDS.In sertCommand = oCommandBuilder .GetInsertComma nd

daProductsDS.Up dateCommand = oCommandBuilder .GetUpdateComma nd

'Apply the updates

daProductsDS.Up date(dsNorthwin d, "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

Jul 21 '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******** ******@TK2MSFTN GP09.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.publi c.dotnet.framew ork.adonet.

Ron Allen

"Vijay Balki" <vi****@softe ch-usa.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(C onnectionString )

con.Open()

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

Dim oCommandBuilder As New SqlCommandBuild er(daProductsDS )

daProductsDS.De leteCommand = oCommandBuilder .GetDeleteComma nd

daProductsDS.In sertCommand = oCommandBuilder .GetInsertComma nd

daProductsDS.Up dateCommand = oCommandBuilder .GetUpdateComma nd

'Apply the updates

daProductsDS.Up date(dsNorthwin d, "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


Jul 21 '05 #4

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

Similar topics

1
1960
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 inserts must be done parent first, then child. This is generally accomplished by using DataSet.GetChanges(DataRowState.Deleted) and DataSet.GetChanges(DataRowState.Inserted) respectively. My question is: What is the best way after this to sync up...
4
2615
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, then written to the database... or at least that's what's supposed to be happening. Unfortunately, I've discovered that while it appears that when I create a new record/row I'm successfully updating the Access database, once the Update is...
4
10031
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 have a .getxml method (unlike the dataset). any ideas? thanks.
0
1970
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 single record from database for updates to existing record is necessay. Now I want to take the dataset that has been updated and update the collection Dataset. Once everything is done I send the whole thing at one time to database to do...
4
2217
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 RAM? Or a portion of them is fetched to RAM for processing? Thanks.
4
1769
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, delete, insert commands) after already specifying a very basic select command that consists of two fields. All procedures are created fine and i'm able to save the xsd file. Then I create a new gridview in which i use the xsd file object as my
3
1675
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 SqlConnection(ConnectionString) con.Open() Dim daProductsDS As New SqlDataAdapter("Select * from ProductsDS", con) Dim oCommandBuilder As New SqlCommandBuilder(daProductsDS)
4
1850
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 the record, this all works just fine, then the user closes the detail form and ofcourse I want to show his modifications in the grid form, but now my question is what's the best way to do this without completely reloading the dataset that holds the...
2
2778
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. My application fetches data from parent tables and put data in separate tables (chils tables) of application. I am using dataset to fetch data from parent tables and insert/update data in child table.
0
8449
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8876
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.