System.Data.OleDb Namespace OleDbCommand Class
Sets or retrieves a value indicating how the CommandText property is to be interpreted.
Script |
OleDbCommand.CommandType [ = enumValue ] |
The property is read/write with a default value of Text.
IDbCommand.CommandType
When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. The command executes this stored procedure when you call one of the Execute methods.
When the CommandType property is set to TableDirect, the CommandText property should be set to the name of the table or tables to be accessed. The user may be required to use escape character syntax if any of the tables named contain any special characters. All rows and columns of the named table or tables will be returned when you call one of the Execute methods.
You cannot set the Connection, CommandType and CommandText properties if the current connection is performing an execute or fetch operation.
The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL Statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark ( ? ) placeholder must be used. For example:
SELECT * FROM Customers WHERE CustomerID = ?
As a result, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter.
For more information see Using Stored Procedures with a Command.
The following example illustrates how the CommandType property can be set to call a procedure stored in an SQL Server™ database ( under version 7.0 ).
OleDbConnection myConn = new OleDbConnection
( "Provider=SQLOLEDB; Data Source=localhost; " +
"Integrated Security=SSPI; Initial Catalog=northwind" );
OleDbCommand salesCmd = new OleDbCommand
( "SalesByCategory", myConn );
salesCmd.CommandType = CommandType.StoredProcedure;
OleDbParameter myParam = salesCmd.Parameters.Add
( "@CategoryName", OleDbType.NVarChar, 15 );
myParam.Value = "Beverages";
myConn.Open ( );
OleDbDataReader myReader = salesCmd.ExecuteReader ( );
Response.Write ( myReader.GetName ( 0 ) + ", " +
myReader.GetName ( 1 ) + "<br>" );
while ( myReader.Read ( ) ) {
Response.Write ( myReader.GetString ( 0 ) + ", " +
myReader.GetDecimal ( 1 ) + "<br>" );
}
myReader.Close ( );
myConn.Close ( );
Dim myConn As OleDbConnection = New OleDbConnection _
( "Provider=SQLOLEDB; Data Source=localhost; " & _
"Integrated Security=SSPI; Initial Catalog=northwind" )
Dim salesCmd As OleDbCommand = New OleDbCommand _
( "SalesByCategory", myConn )
salesCmd.CommandType = CommandType.StoredProcedure
Dim myParam As OleDbParameter = salesCmd.Parameters.Add _
( "@CategoryName", OleDbType.NVarChar, 15 )
myParam.Value = "Beverages"
myConn.Open ( )
Dim myReader As OleDbDataReader = salesCmd.ExecuteReader ( )
Response.Write ( myReader.GetName ( 0 ) & ", " & _
myReader.GetName ( 1 ) & "<br>" )
Do While myReader.Read ( )
Response.Write ( myReader.GetString ( 0 ) & ", " & _
myReader.GetDecimal ( 1 ) & "<br>" )
Loop
myReader.Close ( )
myConn.Close ( ) |
|
C# |
VB |
OleDbCommand Members CommandText CommandTimeout Connection