System.Data.OleDb Namespace OleDbCommand Class
Returns the OleDbParameterCollection.
Script |
[ OleDbParameterCollection variable = ] OleDbCommand.Parameters |
variable |
The parameters of the SQL statement or stored procedure. |
The property is read/write with no default value.
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.
NOTE: If the parameters in the collection do not match the requirements of the query to be executed, an error may result.
For more information see Using Stored Procedures with a Command.
The following example illustrates using the Parameters collection.
void ParametersDemo ( OleDbConnection myConn,
string query, OleDbParameter [ ] myParamArray ) {
OleDbCommand myCommand = new OleDbCommand ( query, myConn );
myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers " +
"WHERE Country = @Country AND City = @City";
myCommand.Parameters.Add ( myParamArray );
for ( int j = 0; j < myParamArray.Length; j++ ) {
myCommand.Parameters.Add ( myParamArray [ ] );
}
string myMessage = "";
for ( int i = 0; i < myCommand.Parameters.Count; i++ ) {
myMessage += myCommand.Parameters [ ].ToString ( ) + "<br>";
}
Response.Write ( myMessage );
}
Public Sub ParametersDemo ( myConn As OleDbConnection, _
query As String, myParamArray ( ) As OleDbParameter )
Dim myCommand As New OleDbCommand ( query, myConn )
myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers " & _
"WHERE Country = @Country AND City = @City"
myCommand.Parameters.Add ( myParamArray )
Dim j As Integer
For j = 0 To myParamArray.Length - 1
myCommand.Parameters.Add ( myParamArray ( j ) )
Next j
Dim myMessage As String = ""
Dim i As Integer
For i = 0 To myCommand.Parameters.Count - 1
myMessage += myCommand.Parameters ( i ).ToString ( ) & ControlChars.Cr
Next i
Response.Write ( myMessage )
End Sub |
|
C# |
VB |
OleDbCommand Members OleDbParameter