Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Command Question

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
What is the difference between the SqlClient.SqlCommand object and the SqlDataAdapter.SelectCommand object? For instance, if I want to retrieve some rows from a table, the following code works (after instancing connection, command, table, and data-adapter objects):

****************************************
sQueryText = "SELECT * FROM Table"
SQLCatalog = "MyDB"
Server_or_FileName = "MyServer"
With oCn
.ConnectionString = "Initial Catalog=" & SQLCatalog
& ";" & _
"Data Source=" & Server_or_FileName & ";" & _
"Integrated Security=SSPI"
.Open()
End With
oCmd.CommandText = sQueryText
With oDataAdapter
.SelectCommand = oCmd
.SelectCommand.Connection = oCn
.Fill(oTable)
End With
****************************************

But why doesn't this seem to work?:

****************************************
.
.
.
With oDataAdapter
.SelectCommand.CommandType = CommandType.Text
.SelectCommand.CommandText = sQueryText
.SelectCommand.Connection = oCn
.Fill(oTable)
End With
****************************************

Why can't I use the DataAdapter.SelectCommand without first assigning a separate instance of the SQLClient.Command object?
 
With SQLClient.SQLCommand you retrieve only data from a database.
With SQLDataAdapter you can fill DataSets and so
 
Think of it like this:
SelectCommand is a property of the DataAdapter that exposes a command object inside the DataAdapter Object.
Your code doesn't work because that internal command object hasn't been instantiated. If you instantiate it, you can use the DataAdapter without creating a separate command object. Like this:

With oDataAdapter
.SelectCommand = New SqlClient.SqlCommand
.SelectCommand.CommandType = CommandType.Text
.SelectCommand.CommandText = sQueryText
.SelectCommand.Connection = oCn
.Fill(oTable)
End With

You could also do the same with the Connection object of the SelectCommand.
 
Check here for an explanation
thread796-389653 That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top