DataAdapter??

  • Thread starter Thread starter al
  • Start date Start date
A

al

Greetings,

Do I need to explicitly declare an UpdateCommand(write an update SQL
statement) in the Dataadapter object if I want to make a change in a
datagrid updateable in the database, or will the dataadapter
implicitly check for modifications in the Dataset and post the changes
back to the database???

MTIA,
Grawsha
 
You either have to use the DataAdapterConfiguration wizard (which generates
update, insert, select etc) for you provided you have a primary key on the
table. Your other options are use a commandbuilder object which infers
Update/Insert/Delete logic from your Select statement or you have to write
your own logic.

In the first two cases, you don't technically have to 'write' your own CRUD
logic, but something has to do it, either you or a tool.

Bill Vaughn has a superb article available on his web site that should help
you out...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/commandbuilder.asp
 
Yes, either explicitly write an UpdateCommand or use the CommandBuilder. I
think writing your own command is probably better (I've not had much luck
with the CommandBuilder myself). The DataAdapter won't update any rows if it
doesn't have an UpdateCommand.
 
Matt Burland said:
Yes, either explicitly write an UpdateCommand or use the CommandBuilder. I
think writing your own command is probably better (I've not had much luck
with the CommandBuilder myself). The DataAdapter won't update any rows if it
doesn't have an UpdateCommand.


How come in MSDN (Visual Basic and Visual C# Concepts,
Database Updates from Datasets), they say:

"After changes have been made in a dataset, you can transmit the
changes to a data source. Most commonly, you do this by calling the
Update method of a data adapter. The method loops through each record
in a data table, determines what type of update is required (update,
insert, or delete), if any, and then executes the appropriate
command."

There is no mention of the UpdateCommand???
 
Yes, you must set the UpdateCommand else the DA has no idea how to set
update. Do this manually, with the data wizard or with commandbuilder. I
suggest using the data wizard as a starting point.
 
If you think about it for a second, it really makes sense. How does it know
what to do unless you explicitly tell it. How I update Table1 may be
totally different to how you wish to update Table1.

This is much better than ADO in that you know have control over your update
commands.

al said:
"Matt Burland" <wjousts@[nospam]hotmail.com> wrote in message
Yes, either explicitly write an UpdateCommand or use the CommandBuilder. I
think writing your own command is probably better (I've not had much luck
with the CommandBuilder myself). The DataAdapter won't update any rows if it
doesn't have an UpdateCommand.


How come in MSDN (Visual Basic and Visual C# Concepts,
Database Updates from Datasets), they say:

"After changes have been made in a dataset, you can transmit the
changes to a data source. Most commonly, you do this by calling the
Update method of a data adapter. The method loops through each record
in a data table, determines what type of update is required (update,
insert, or delete), if any, and then executes the appropriate
command."

There is no mention of the UpdateCommand???
 
al said:
How come in MSDN (Visual Basic and Visual C# Concepts,
Database Updates from Datasets), they say:

"After changes have been made in a dataset, you can transmit the
changes to a data source. Most commonly, you do this by calling the
Update method of a data adapter. The method loops through each record
in a data table, determines what type of update is required (update,
insert, or delete), if any, and then executes the appropriate
command."

There is no mention of the UpdateCommand???

Because they are right, you call the Update method of your data adapter to
update the table, but that is *after* you have set the UpdateCommand of the
adapter. You set the UpdateCommand once, and you can then call Update as
many times as you want. The UpdateCommand normally acts as a template for
the SQL command that actually gets sent to the database by the Update
method. You specify the text of the UpdateCommand, including parameters that
will be pulled from each DataRow. Then you tell it how to map the data in
each row into the SQL command. Once you've done this when you call Update
the DataAdapter will loop through each record, check to see if it needs
updating, and if it does it'll use the UpdateCommand to pull values out of
your record and construct an SQL statement, this then gets sent to your
database.
 
I agree with you Al. ADO classic generated the UPDATE statement on the fly
but ADO.NET does not--it depends on either the CommandBuilder or your own
design-time efforts to create the appropriate commands. It IS confusing.

--
____________________________________
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

al said:
"Matt Burland" <wjousts@[nospam]hotmail.com> wrote in message
Yes, either explicitly write an UpdateCommand or use the CommandBuilder. I
think writing your own command is probably better (I've not had much luck
with the CommandBuilder myself). The DataAdapter won't update any rows if it
doesn't have an UpdateCommand.


How come in MSDN (Visual Basic and Visual C# Concepts,
Database Updates from Datasets), they say:

"After changes have been made in a dataset, you can transmit the
changes to a data source. Most commonly, you do this by calling the
Update method of a data adapter. The method loops through each record
in a data table, determines what type of update is required (update,
insert, or delete), if any, and then executes the appropriate
command."

There is no mention of the UpdateCommand???
 
Al,

I agree that the wording is clumsy, however, if you take it literally it's
correct -- the method "executes the appropriate command". What commands are
there? The insert, update and delete commands. And where do the docs
(elsewhere) say those commands come from? You, or the Command Builder, or
the result of executing a wizard.

You're right -- technical authors frequently assume too much and don't make
the extra effort to think of what they are writing from the perspective of
people who are learning the ropes -- the very people most likely to be
reading the material closely. This passage would be much clearer with only
minor changes. For instance, it would have been much clearer to say "...
then executes the SQL statement specified in the UpdateCommand,
InsertCommand or DeleteCommand property, as appropriate for each record."
This, with cross-reference links to those properties and perhaps to an
overview topic on command setup, would have made all the difference.

--Bob
 
Back
Top