System.Data Namespace IDbCommand Class
Sets or retrieves a value indicating how the CommandText property is to be interpreted.
Script |
IDbCommand.CommandType [ = enumValue ] |
The property is read/write with a default value of Text.
When you set the CommandType property to StoredProcedure, you should set the CommandText property to the name of the stored procedure. The command executes this stored procedure when you call one of the Execute methods.
The following example illustrates how the CommandType property can be set to call a procedure stored in an SQL Server™ database.
SqlConnection myConn = new SqlConnection
( "Data Source=localhost; Integrated Security=SSPI;" +
"Initial Catalog=northwind" );
SqlCommand salesCmd = new SqlCommand ( "SalesByCategory", myConn );
salesCmd.CommandType = CommandType.StoredProcedure;
SqlParameter myParam = salesCmd.Parameters.Add
( "@CategoryName", SqlDbType.NVarChar, 15 );
myParam.Value = "Beverages";
myConn.Open ( );
SqlDataReader myReader = salesCmd.ExecuteReader ( );
while ( myReader.Read ( ) ) {
Response.Write ( myReader.GetString ( 0 ) + ", " +
myReader.GetDecimal ( 1 ) + "<br>" );
}
myReader.Close ( );
myConn.Close ( );
Dim myConn As SqlConnection = New SqlConnection _
( "Data Source=localhost; Integrated Security=SSPI;" & _
"Initial Catalog=northwind" )
Dim salesCmd As New SqlCommand ( "SalesByCategory", myConn )
salesCmd.CommandType = CommandType.StoredProcedure
Dim myParam As SqlParameter = salesCmd.Parameters.Add _
( "@CategoryName", SqlDbType.NVarChar, 15 )
myParam.Value = "Beverages"
myConn.Open ( )
Dim myReader As SqlDataReader = salesCmd.ExecuteReader ( )
Do While myReader.Read ( )
Response.Write ( myReader.GetString ( 0 ) & ", " & _
myReader.GetDecimal ( 1 ) & "<br>" )
Loop
myReader.Close ( )
myConn.Close ( ) |
|
C# |
VB |
IDbCommand Members CommandText CommandTimeout Connection