Home All Groups Group Topic Archive Search About

difference between dataAdapter.InsertCommand/dataAdapter.SelectCom

Author
9 Nov 2006 4:58 PM
Rich
What is the diffeence bewtween a dataAdapter.InsertCommand and
dataAdapter.SelectCommand (and dataAdapter.UpdateCommand for that matter)?

Dim da As SqlDataAdapter
conn.Open
da.SelectCommand = New SqlCommand
da.SelectCommand.Connectoin = conn
da.SelectCommand.CommandType = Command.Text
da.SelectCommand.CommandText = "insert Into tbl1 Select * from tbl2"
da.SelectCommand.ExecuteNonQuery

Or

Dim da As SqlDataAdapter
conn.Open
da.InsertCommand = New SqlCommand
da.InsertCommand.Connectoin = conn
da.InsertCommand.CommandType = Command.Text
da.InsertCommand.CommandText = "insert Into tbl1 Select * from tbl2"
da.InsertCommand.ExecuteNonQuery

Is there any differece between these 2 dataAdapters?  It looks to me like
they both perform the same operation. 

Thanks,
Rich

Author
9 Nov 2006 6:53 PM
Rich
I believe the answer to my question is that you can use the
dataAdapter.Update method with dataAdapter.InsertCommand and
dataAdapter.Update command, but you can only use dataAdapter.Fill with the
dataAdapter.SelectCommand.

Any additional comments appreciated.

Show quoteHide quote
"Rich" wrote:

> What is the diffeence bewtween a dataAdapter.InsertCommand and
> dataAdapter.SelectCommand (and dataAdapter.UpdateCommand for that matter)?
>
> Dim da As SqlDataAdapter
> conn.Open
> da.SelectCommand = New SqlCommand
> da.SelectCommand.Connectoin = conn
> da.SelectCommand.CommandType = Command.Text
> da.SelectCommand.CommandText = "insert Into tbl1 Select * from tbl2"
> da.SelectCommand.ExecuteNonQuery
>
> Or
>
> Dim da As SqlDataAdapter
> conn.Open
> da.InsertCommand = New SqlCommand
> da.InsertCommand.Connectoin = conn
> da.InsertCommand.CommandType = Command.Text
> da.InsertCommand.CommandText = "insert Into tbl1 Select * from tbl2"
> da.InsertCommand.ExecuteNonQuery
>
> Is there any differece between these 2 dataAdapters?  It looks to me like
> they both perform the same operation. 
>
> Thanks,
> Rich
Author
10 Nov 2006 12:30 PM
Bart Mermuys
Hi,

"Rich" <R***@discussions.microsoft.com> wrote in message
news:8089CC56-ED65-48A5-AC1F-3CD991AAC4B0@microsoft.com...
>I believe the answer to my question is that you can use the
> dataAdapter.Update method with dataAdapter.InsertCommand and
> dataAdapter.Update command, but you can only use dataAdapter.Fill with the
> dataAdapter.SelectCommand.

DataAdapter.Update(DataTable) will use UpdateCommand for each row that is
modified, InsertCommand for each new row and DeleteCommand for each deleted
row.

DataAdapter.Fill(DataTable) will use the SelectCommand.

Notice that these Update, Insert, Delete Command's are not supposed to be
used like you do.  Their SQL queries should include parameters which are
linked to the field (column) names in the DataTable and you should not call
ExecuteNonQuery on them.

If you want to execute a query like yours, then use a SqlCommand instead,
eg. :

Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandType = Command.Text
cmd.CommandText = "insert Into tbl1 Select * from tbl2"
cmd.ExecuteNonQuery()


HTH,
Greetings


Show quoteHide quote
>
> Any additional comments appreciated.
>
> "Rich" wrote:
>
>> What is the diffeence bewtween a dataAdapter.InsertCommand and
>> dataAdapter.SelectCommand (and dataAdapter.UpdateCommand for that
>> matter)?
>>
>> Dim da As SqlDataAdapter
>> conn.Open
>> da.SelectCommand = New SqlCommand
>> da.SelectCommand.Connectoin = conn
>> da.SelectCommand.CommandType = Command.Text
>> da.SelectCommand.CommandText = "insert Into tbl1 Select * from tbl2"
>> da.SelectCommand.ExecuteNonQuery
>>
>> Or
>>
>> Dim da As SqlDataAdapter
>> conn.Open
>> da.InsertCommand = New SqlCommand
>> da.InsertCommand.Connectoin = conn
>> da.InsertCommand.CommandType = Command.Text
>> da.InsertCommand.CommandText = "insert Into tbl1 Select * from tbl2"
>> da.InsertCommand.ExecuteNonQuery
>>
>> Is there any differece between these 2 dataAdapters?  It looks to me like
>> they both perform the same operation.
>>
>> Thanks,
>> Rich
Author
10 Nov 2006 3:34 PM
Brian Tkatch
Rich wrote:
Show quoteHide quote
> What is the diffeence bewtween a dataAdapter.InsertCommand and
> dataAdapter.SelectCommand (and dataAdapter.UpdateCommand for that matter)?
>
> Dim da As SqlDataAdapter
> conn.Open
> da.SelectCommand = New SqlCommand
> da.SelectCommand.Connectoin = conn
> da.SelectCommand.CommandType = Command.Text
> da.SelectCommand.CommandText = "insert Into tbl1 Select * from tbl2"
> da.SelectCommand.ExecuteNonQuery
>
> Or
>
> Dim da As SqlDataAdapter
> conn.Open
> da.InsertCommand = New SqlCommand
> da.InsertCommand.Connectoin = conn
> da.InsertCommand.CommandType = Command.Text
> da.InsertCommand.CommandText = "insert Into tbl1 Select * from tbl2"
> da.InsertCommand.ExecuteNonQuery
>
> Is there any differece between these 2 dataAdapters?  It looks to me like
> they both perform the same operation.
>
> Thanks,
> Rich

The DataAdapter is meant to make a DataGrid work like it was a real
table. That is, updating the grid should issue an UPDATE statement to
the real underlying table. And so on. By allowing these different
commands, there is a methodical way of keeping up this charade.

Ultimately, however, they are just Commands, and can be used for
whatever you'd like. But, it would be confusing to another programmer
reviewing your work.

B.