By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

difference between dataAdapter.InsertCommand/dataAdapter.SelectCom

P: n/a
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
Nov 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"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
Nov 9 '06 #2

P: n/a
Hi,

"Rich" <Ri**@discussions.microsoft.comwrote in message
news:80**********************************@microsof t.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

>
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

Nov 10 '06 #3

P: n/a
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
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.

Nov 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.