To support the various commands you can perform on a Microsoft Access database, the System.Data.OleDb namespace provides the OleDbCommand class. To use it, you can declare a variable of type OleDbCommand using one of its constructors.
The OleDbCommand class is equipped with four constructors. The default constructor allows you to initiate a command without specifying what action would be taken. The action to perform is created as a string statement. This action is represented by the CommandText property of the OleDbCommand class, which is of type string. |
|
If you want to use the default constructor, you can then create a string that would carry the action to perform. Once the string is ready, you can assign it the CommandText property. This would be done as follow: OleDbCommand CommandToExecute = new OleDbCommand(); string strCommandToExecute = "Blah Blah Blah"; CommandToExecute.CommandText = strCommandToExecute; After creating the action that would be performed, you must specify what connection would carry it. To do this, you can first create a OleDbConnection object. To provide it to the command, the OleDbCommand class is equipped with a property named Connection that is of type OleDbConnection. After creating a OleDbConnection object, to provide it to the command, you can assign it to the OleDbCommand.Connection property. This would be done as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.OleDb; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;" + "Data Source=" + Server.MapPath("App_Data/exercise.mdb"))) { OleDbCommand commandToExecute = new OleDbCommand(); string strCommandToExecute = "Blah Blah Blah"; connection.Open(); commandToExecute.Connection = connection; commandToExecute.CommandText = strCommandToExecute; } } } Instead of declaring a OleDbCommand variable and the command text separately, as an alternative, you can define the command text when declaring the OleDbCommand variable. You can use the second constructor of the OleDbCommand class. The syntax of this constructor is: public OleDbCommand(string cmdText); After using this constructor, you must specify what connection would carry the action. To do this, you can assign a OleDbConnection object to the Connection property of your OleDbCommand. Instead of assigning a OleDbConnection object to the OleDbCommand.Connection property, you can specify what connection would carry the action at the same time you are creating the command. To specify the connection when declaring the OleDbCommand variable, you can use the third constructor of this class. Its syntax is: public OleDbCommand(string cmdText, OleDbConnection connection); The second argument to this constructor is an established connection you would have defined. If you had initiated the action using the default constructor of the OleDbCommand class, you can assign a OleDbConnection object to the Connection property of the OleDbCommand class.
After establishing a connection and specifying what command needs to be carried, you can execute it. This is done using the ExecuteNonQuery() method of the OleDbCommand class: public override int ExecuteNonQuery(); This method does not take any argument. The OleDbCommand object that calls it must have prepared a valid command.
The OleDbCommand.CommandTimeOut property allows you to specify the time to wait before trying to execute a command. The default value of this property is 30 (seconds). If you want a different value, assign it to your OleDbCommand variable.
In this and the next few lessons, all of the commands we perform will be communicated as strings. When we study (stored) procedures, we will see other types of commands. To allow you to specify the type of command you want to perform, the OleDbCommand class is equipped with the CommandType property, which is based on the CommandType enumeration. The CommandType enumeration has three members: StoredProcedure, TableDirect, and Text. For a OleDbCommand object, the default value is Text.
A data command is used to initiate an action to perform on a database. To read data of a database, one of the objects you can use is called a data reader. To support data readers, the .NET Framework provides, for a Microsoft Access database, a class named OleDbDataReader. To get a data reader, you can declare a variable of type OleDbDataReader. This class does not have a constructor. This means that, to use it, you must (directly) specify where it would read its data. To provide data to the reader, the OleDbCommand class is equipped with the ExecuteReader() method that is overloaded with two versions. The simplest version of this method uses the following syntax: public OleDbDataReader ExecuteReader(); Based on this, before using a data reader, you should first create a command that would specify how data would be acquired. Once the data is read, you can pass it to the data reader by assigning the result of a call to a OleDbCommand.ExecuteReader() method to a OleDbDataReader object.
Once data is supplied to the reader, you can access it, one value at a time, from top to bottom. To access data that the reader acquired, you can call its Read() method whose syntax is: public override bool Read(); As mentioned already, the Read() method simply reads a value and moves on. When reading the values, as mentioned already many times, the data reader reads one value at a time and moves to the next. |
|
|
||
Home | Copyright © 2009-2016, FunctionX, Inc. | Next |
|