Export (0) Print
Expand All

OleDbParameter Class

Represents a parameter to an OleDbCommand and optionally its mapping to a DataSet column. This class cannot be inherited.

Namespace: System.Data.OleDb
Assembly: System.Data (in system.data.dll)

public sealed class OleDbParameter : DbParameter, ICloneable, IDbDataParameter, IDataParameter
public final class OleDbParameter extends DbParameter implements ICloneable, IDbDataParameter, 
	IDataParameter
public final class OleDbParameter extends DbParameter implements ICloneable, IDbDataParameter, 
	IDataParameter

The OLE DB.NET Framework Data Provider uses positional parameters that are marked with a question mark (?) instead of named parameters.

When querying an Oracle database using the Microsoft OLE DB Provider for Oracle (MSDAORA) and the OLE DB.NET Framework Data Provider, using the LIKE clause to query values in fixed-length fields may not return all expected matches. The reason is that when Oracle matches values for fixed-length fields in a LIKE clause, it matches the entire length of the string, including any padding trailing spaces. For example, if a table in an Oracle database contains a field named "Field1" that is defined as char(3), and you enter the value "a" into a row of that table, the following code does not return the row.

string queryString = "SELECT * FROM Table1 WHERE Field1 LIKE ?";
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a";
OleDbDataReader reader = command.ExecuteReader();

This is because Oracle stores the column value as "a " (padding "a", with trailing spaces, to the fixed field length of 3), which Oracle does not treat as a match for the parameter value of "a" in the case of a LIKE comparison of fixed-length fields.

To resolve this problem, append a percentage ("%") wildcard character to the parameter value ("a%"), or use an SQL = comparison instead.

The following example creates multiple instances of OleDbParameter through the OleDbParameterCollection collection within the OleDbDataAdapter. These parameters are used to select data from the data source and place the data in the DataSet. This example assumes that a DataSet and an OleDbDataAdapter have already been created by using the appropriate schema, commands, and connection.

public DataSet GetDataSetFromAdapter(
    DataSet dataSet, string connectionString, string queryString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter =
            new OleDbDataAdapter(queryString, connection);

        // Set the parameters.
        adapter.SelectCommand.Parameters.Add(
            "@CategoryName", OleDbType.VarChar, 80).Value = "toasters";
        adapter.SelectCommand.Parameters.Add(
            "@SerialNum", OleDbType.Integer).Value = 239;

        // Open the connection and fill the DataSet.
        try
        {
            connection.Open();
            adapter.Fill(dataSet);
        
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        
        // The connection is automatically closed when the
        // code exits the using block.
    
    return dataSet;


using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
        //        string x = "Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind";
    

    public DataSet GetDataSetFromAdapter(
        DataSet dataSet, string connectionString, string queryString)
    {
        using (OleDbConnection connection =
                   new OleDbConnection(connectionString))
        {
            OleDbDataAdapter adapter =
                new OleDbDataAdapter(queryString, connection);

            // Set the parameters.
            adapter.SelectCommand.Parameters.Add(
                "@CategoryName", OleDbType.VarChar, 80).Value = "toasters";
            adapter.SelectCommand.Parameters.Add(
                "@SerialNum", OleDbType.Integer).Value = 239;

            // Open the connection and fill the DataSet.
            try
            {
                connection.Open();
                adapter.Fill(dataSet);
            
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            
            // The connection is automatically closed when the
            // code exits the using block.
        
        return dataSet;
    

System.Object
   System.MarshalByRefObject
     System.Data.Common.DbParameter
      System.Data.OleDb.OleDbParameter

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft