difference between dataAdapter.InsertCommand/dataAdapter.SelectCom

G

Guest

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
 
G

Guest

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.
 
B

Bart Mermuys

Hi,

Rich said:
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
 
B

Brian Tkatch

Rich said:
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.
 

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