How to use InsertCommand,UpdateCommand,DeleteCommand

B

Bharat

Hi all,

Can anyone explain me how to use the Dataadapter methods
like insertcommand,updatecommand and deletecommand with
some clear samples. What I wanna know is how will you add
a new record or edit an existing record and so on via
these methods.

Any help appreciated.

Thanks

Bharat.NET
 
G

Greg

Far too much to cover in this forum. A good starting place is msdn or sdk.
Do a search on DataAdapter.Update. Long story short, you have 3 methods of
building your update sql:

DataWizard
CommandBuilder
Do it manually

Doing it manually is best. I would start with the DataWizard so you get an
idea of how it all works.
 
D

David Sceppa

Here's a very condensed summary. For more information, pick
up a good book on ADO.NET. I recommend "Microsoft ADO.NET" from
Microsoft Press, but I'm admittedly biased.

The DataRow class lets you manage a set of changes to a row
of data, allowing you to access the current and original values
for each column. You can also check the DataRow object's
RowState property to determine the type of pending change stored
in the row - insert, update, delete.

You could use all of this information to build your own
update queries for each pending change. To simplify the process,
you might want to build a separate parameterized query for each
type of change - insert, update, and delete. These queries may
be standard SQL INSERT, UPDATE, and DELETE queries, or they may
be calls to stored procedures. Once you've created these
queries, you could process all modified rows and assign values
from the DataRow to the parameters of the appropriate query to
submit the pending change.

DataAdapters are designed to simplify this process. When
you call DataAdapter.Update and supply a DataTable, the
DataAdapter walks the rows and executes the query in the
appropriate Command property (InsertCommand, UpdateCommand,
DeleteCommand) based on each DataRow's RowState.

Parameters also simplify this process. Set the Parameter
object's SourceColumn and SourceVersion properties to bind the
parameter to a particular column and version of data in the
DataRow.

You can supply updating logic through code at run-time by
setting the DataAdapter's InsertCommand, UpdateCommand, and
DeleteCommand or you can use a CommandBuilder to generate this
logic for you dynamically at run-time. Using your own code will
give you better control and better performance. Relying on a
CommandBuilder requires less code, but requires that the
CommandBuilder ask the back-end for extended schema information
such as table and column names, key column information, etc. that
hampers performance.

Visual Studio .NET includes wizards that generate updating
logic for you. The wizard generates updating logic similar to
that of CommandBuilders, except that this logic is stored in your
code, resulting in better performance.

For code examples in C# and VB.NET using the SqlClient and
OLE DB .NET Data Providers, see "Updating the Database with a
DataAdapter and the DataSet" in the .NET Framework SDK under

.NET Framework SDK
Programming with the .NET Framework
Accessing Data with ADP.NET
Using .NET Data Providers to Access Data


I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top