Having a connection open doesn’t achieve much in itself. We need a command object to retrieve data, insert, update and delete records.
We’ll declare an OledbCommand variable that’s fit for the purpose of holding a command object.
After declaring the command variable we need to create a command object. We’ll create our Command object using the New Keyword and at the same time the Command object accepts 2 parameters namely the SQL Statement and the connection object. The connection object holds all the connection information to the database that our command object needs.
Once we’ve created our command object we can assign it to our command variable ‘command’.
Now we need to return the records from the database into a DataReader object.
First of all lets declare our OledbDatareader variable ‘DataReader’. Then we’ll fill the datareader by calling the ExecuteReader method of our command object. You’ll notice that rather than using the New Keyword as we do with other ADO.NET objects we use the ExecuteReader method to create our DataReader object. The Command object references the connection and the SQL statement necessary for the OledbDataReader to obtain data.
We could iterate through the records in the DataReader row by row but an easier alternative is to place a Gridview control in our code within the HTML body element and bind the DataReader to the Gridview.
We set the Datasource property of the Gridview to our DataReader and then use the DataBind method of our Gridview to bind the DataReader’s results.
Lastly let's close the connection object.
<< Reading Records with ASP.NET Part 2 |
Reading Records with ASP.NET Part 4>> |
Get the best asp web hosting provider now and save 30%
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.