OleDbParameter 类

定义

表示 OleDbCommand 的参数,还可以是参数到 DataSet 列的映射。Represents a parameter to an OleDbCommand and optionally its mapping to a DataSet column. 无法继承此类。This class cannot be inherited.

public ref class OleDbParameter sealed : System::Data::Common::DbParameter, ICloneable
[System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbParameter/OleDbParameterConverter))]
[System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbParameterConverter))]
public sealed class OleDbParameter : System.Data.Common.DbParameter, ICloneable
type OleDbParameter = class
    inherit DbParameter
    interface ICloneable
    interface IDbDataParameter
    interface IDataParameter
Public NotInheritable Class OleDbParameter
Inherits DbParameter
Implements ICloneable
继承
OleDbParameter
属性
实现

示例

下面的示例通过 OleDbDataAdapter中的 OleDbParameterCollection 集合创建多个 OleDbParameter 实例。The following example creates multiple instances of OleDbParameter through the OleDbParameterCollection collection within the OleDbDataAdapter. 这些参数用于从数据源中选择数据,并将数据放入 DataSetThese parameters are used to select data from the data source and place the data in the DataSet. 此示例假设已使用适当的架构、命令和连接创建了 DataSetOleDbDataAdapterThis 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;
}
Public Function GetDataSetFromAdapter( _
    ByVal dataSet As DataSet, ByVal connectionString As String, _
    ByVal queryString As String) As DataSet

    Using connection As New OleDbConnection(connectionString)
        Dim adapter As 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 ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using

    Return dataSet
End Function

注解

OLE DB.NET Framework 数据提供程序使用以问号(?)标记的位置参数,而不是命名参数。The OLE DB.NET Framework Data Provider uses positional parameters that are marked with a question mark (?) instead of named parameters.

使用 Oracle 的 Microsoft OLE DB 提供程序(MSDAORA)和 OLE DB.NET Framework 数据提供程序查询 Oracle 数据库时,如果使用 LIKE 子句来查询固定长度字段中的值,则可能不会返回所有预期匹配项。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. 原因在于,当 Oracle 在 LIKE 子句中匹配固定长度字段的值时,它将与字符串的整个长度匹配,其中包括任何空白尾随空格。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. 例如,如果 Oracle 数据库中的某个表包含一个名为 "Field1" 的字段,该字段定义为 char(3),并且您在该表的行中输入值 "a",则下面的代码不会返回该行。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.

Dim queryString As String = "SELECT * FROM Table1 WHERE Field1 LIKE ?"  
Dim command As OleDbCommand = New OleDbCommand(queryString, connection)  
command.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a"  
Dim reader As OleDbDataReader = command.ExecuteReader()  
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();  

这是因为 Oracle 将列值存储为 "a" (填充 "a",尾随空格,固定字段长度为3),在 LIKE 固定长度字段比较的情况下,Oracle 不会将其视为 "a" 参数值的匹配项。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.

若要解决此问题,请向参数值("a%")追加一个百分比(%)通配符,或者改为使用 SQL = 比较。To resolve this problem, append a percentage ("%") wildcard character to the parameter value ("a%"), or use an SQL = comparison instead.

构造函数

OleDbParameter()

初始化 OleDbParameter 类的新实例。Initializes a new instance of the OleDbParameter class.

OleDbParameter(String, Object)

初始化使用新 OleDbParameter 的参数名称和值的 OleDbParameter 类的新实例。Initializes a new instance of the OleDbParameter class that uses the parameter name and the value of the new OleDbParameter.

OleDbParameter(String, OleDbType)

初始化使用参数名称和数据类型的 OleDbParameter 类的新实例。Initializes a new instance of the OleDbParameter class that uses the parameter name and data type.

OleDbParameter(String, OleDbType, Int32)

初始化使用参数名称、数据类型和长度的 OleDbParameter 类的新实例。Initializes a new instance of the OleDbParameter class that uses the parameter name, data type, and length.

OleDbParameter(String, OleDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object)

初始化 OleDbParameter 类的新实例,该类使用参数名称、数据类型、长度、源列名、参数方向、数值精度和其他属性。Initializes a new instance of the OleDbParameter class that uses the parameter name, data type, length, source column name, parameter direction, numeric precision, and other properties.

OleDbParameter(String, OleDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object)

初始化 OleDbParameter 类的新实例,该类使用参数名称、数据类型、长度、源列名、参数方向、数值精度和其他属性。Initializes a new instance of the OleDbParameter class that uses the parameter name, data type, length, source column name, parameter direction, numeric precision, and other properties.

OleDbParameter(String, OleDbType, Int32, String)

初始化使用参数名称、数据类型、长度和源列名称的 OleDbParameter 类的新实例。Initializes a new instance of the OleDbParameter class that uses the parameter name, data type, length, and source column name.

属性

DbType

获取或设置参数的 DbTypeGets or sets the DbType of the parameter.

Direction

获取或设置一个值,该值指示参数是只可输入、只可输出、双向还是存储过程返回值参数。Gets or sets a value that indicates whether the parameter is input-only, output-only, bidirectional, or a stored procedure return-value parameter.

IsNullable

获取或设置一个值,该值指示参数是否接受 null 值。Gets or sets a value that indicates whether the parameter accepts null values.

Offset
OleDbType

获取或设置参数的 OleDbTypeGets or sets the OleDbType of the parameter.

ParameterName

获取或设置 OleDbParameter 的名称。Gets or sets the name of the OleDbParameter.

Precision

获取或设置用于表示 Value 属性的最大位数。Gets or sets the maximum number of digits used to represent the Value property.

Scale

获取或设置所解析的 Value 的小数位数。Gets or sets the number of decimal places to which Value is resolved.

Size

获取或设置列中数据的最大大小(字节)。Gets or sets the maximum size, in bytes, of the data within the column.

SourceColumn

获取或设置源列的名称,该源列映射到 DataSet 并用于加载或返回 ValueGets or sets the name of the source column mapped to the DataSet and used for loading or returning the Value.

SourceColumnNullMapping

获取或设置一个值,该值指示源列是否可以为 null。Gets or sets a value which indicates whether the source column is nullable. 通过此操作,DbCommandBuilder 能够为可以为 null 的列正确地生成 Update 语句。This allows DbCommandBuilder to correctly generate Update statements for nullable columns.

SourceVersion

获取或设置在加载 DataRowVersion 时使用的 ValueGets or sets the DataRowVersion to use when you load Value.

Value

获取或设置参数的值。Gets or sets the value of the parameter.

方法

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
ResetDbType()

重置与此 OleDbParameter 关联的类型。Resets the type associated with this OleDbParameter.

ResetOleDbType()

重置与此 OleDbParameter 关联的类型。Resets the type associated with this OleDbParameter.

ToString()

获取一个包含 ParameterName 的字符串。Gets a string that contains the ParameterName.

显式接口实现

ICloneable.Clone()

有关此成员的说明,请参阅 Clone()For a description of this member, see Clone().

IDataParameter.SourceVersion (继承自 DbParameter)
IDbDataParameter.Precision

指示数值参数的精度。Indicates the precision of numeric parameters.

(继承自 DbParameter)
IDbDataParameter.Scale

有关此成员的说明,请参阅 ScaleFor a description of this member, see Scale.

(继承自 DbParameter)

适用于

另请参阅