Now that we can establish a connection, we need to be able to execute statements against the database. The simplest and most direct route for this is though the ADO and SQL Command objects.
[VB]
Dim SQlStmt As String = "SELECT * FROM Customers" Dim myCommand As ADOCommand = New ADOCommand(SQLStmt, myConnection)
[C#]
String SQLStmt = " SELECT * FROM Customers"; ADOCommand myCommand = new ADOCommand(SQLStmt, myConnection);
[VB]
Dim SQlStmt As String = "SELECT * FROM Customers" Dim myCommand As SQLCommand = New SQLCommand(SQLStmt, myConnection)
[C#]
String SQLStmt = " SELECT * FROM Customers"; SQLCommand myCommand = new SQLCommand(SQLStmt, myConnection);
With a command and connection, we can execute the query against the database. But first, one more object must be created before calling the command Execute method with the object as a parameter.
[VB]
Dim myReader As ADODataReader = Nothing myCommand.Execute(myReader)
[C#]
ADODataReader myReader = null; myCommand.Execute(out myReader);
[VB]
Dim myReader As SQLDataReader = Nothing myCommand.Execute(myReader)
[C#]
SQLDataReader myReader = null; myCommand.Execute(out myReader);
The example declares a variable that will hold the instantiated datareader that is returned as a parameter of the command's execute method.