Using Command Objects

The OleDbCommand and SqlCommand classes represent the objectification of stored procedures and SQL. You need a way to send SQL text, stored procedures, and their incumbent parameters to data providers, and in .NET, you use the command object for this purpose. Listing 11.7 provides an example showing how to use a command object. Chapter 12 includes an example using a stored procedure.

Listing 11.7 Filling a DataSet by Using SQL and an OleDbCommand Object
 Private Sub InitializeGridWithCommand()   Dim Connection As OleDbConnection = _     New OleDbConnection(Database.ConnectionString)   Dim Command As OleDbCommand = _     New OleDbCommand("SELECT * FROM CUSTOMERS", Connection)   Dim Adapter As OleDbDataAdapter = _     New OleDbDataAdapter(Command)   Dim Customers As DataTable = New DataTable("Customers")   Adapter.Fill(Customers)   DataGrid1.DataSource = Customers   DataGrid1.DataBind() End Sub 

Notice that Listing 11.7 is almost identical to Listing 11.5. The obvious difference is that I substituted literal SQL text to initialize the adapter with a command object.

Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel © 2008-2017.
If you may any questions please contact us: