2 out of 4 rated this helpful - Rate this topic

OleDbCommand.Parameters Property

Namespace:  System.Data.OleDb
Assembly:  System.Data (in System.Data.dll)
public OleDbParameterCollection Parameters { get; }

Property Value

Type: System.Data.OleDb.OleDbParameterCollection
The parameters of the SQL statement or stored procedure. The default is an empty collection.

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 = ?

Therefore, 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 in the command text.

Note 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 Configuring Parameters and Parameter Data Types (ADO.NET).

The following example creates an OleDbCommand and displays its parameters. To accomplish this, the method is passed an OleDbConnection, a query string that is an SQL SELECT statement, and an array of OleDbParameter objects.


public void CreateMyOleDbCommand(OleDbConnection connection,
    string queryString, OleDbParameter[] parameters) 
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
    command.CommandText = 
        "SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?";
    command.Parameters.Add(parameters);

    for (int j=0; j<parameters.Length; j++)
    {
        command.Parameters.Add(parameters[j]) ;
    }

    string message = "";
    for (int i = 0; i < command.Parameters.Count; i++) 
    {
        message += command.Parameters[i].ToString() + "\n";
    }
    Console.WriteLine(message);
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Is it just me or is the example bonkers?
$0The way I read it, it looks like the author either started a refactoring and never finished. This is the way I interpret it:$0 $0 $0 $0Create the command with the given query text.$0 $0Set the CommandText property to the query text.$0 $0Add the parameters.$0 $0Add the parameters.$0 $0Print out the list of doubled parameters.$0 $0 $0