To support the various commands you can perform on a Microsoft SQL Server database, the System.Data.SqlClient namespace provides the SqlCommand class. To use it, you can declare a variable of type SqlCommand using one of its constructors.
The SqlCommand 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 SqlCommand 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: SqlCommand CommandToExecute = new SqlCommand(); 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 SqlConnection object. To provide it to the command, the SqlCommand class is equipped with a property named Connection that is of type SqlConnection. After creating a SqlConnection object, to provide it to the command, you can assign it to the SqlCommand.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.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strConnection = "Server=(local);" + "Integrated Security=no;" + "User ID=sa;PWD=$outh~@kotA"; SqlConnection connection = new SqlConnection(strConnection); SqlCommand CommandToExecute = new SqlCommand(); string strCommandToExecute = "Blah Blah Blah"; connection.Open(); CommandToExecute.Connection = connection; CommandToExecute.CommandText = strCommandToExecute; connection.Close(); } } Instead of declaring a SqlCommand variable and the command text separately, as an alternative, you can define the command text when declaring the SqlCommand variable. You can use the second constructor of the SqlCommand class. The syntax of this constructor is: public SqlCommand(string cmdText); After using this constructor, you must specify what connection would carry the action. To do this, you can assign a SqlConnection object to the Connection property of your SqlCommand. Here is an example: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strConnection = "Server=(local);" + "Integrated Security=no;" + "User ID=sa;PWD=$outh~@kotA"; SqlConnection connection = new SqlConnection(strConnection); SqlCommand CommandToExecute = new SqlCommand("Blah Blah Blah"); connection.Open(); CommandToExecute.Connection = connection; connection.Close(); } } Instead of assigning a SqlConnection object to the SqlCommand.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 SqlCommand variable, you can use the third constructor of this class. Its syntax is: public SqlCommand(string cmdText, SqlConnection 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 SqlCommand class, you can assign a SqlConnection object to the Connection property of the SqlCommand 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 SqlCommand class: public override int ExecuteNonQuery(); This method does not take any argument. The SqlCommand object that calls it must have prepared a valid command.
The SqlCommand.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 SqlCommand 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 SqlCommand 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 SqlCommand 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 SQL Server database, a class named SqlDataReader. To get a data reader, you can declare a variable of type SqlDataReader. 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 SqlCommand class is equipped with the ExecuteReader() method that is overloaded with two versions. The simplest version of this method uses the following syntax: public SqlDataReader 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 SqlCommand.ExecuteReader() method to a SqlDataReader 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. |
|