OdbcParameter OdbcParameter OdbcParameter OdbcParameter Class

定义

表示 OdbcCommand 的参数,还可以表示它到 DataColumn 的映射。Represents a parameter to an OdbcCommand and optionally, its mapping to a DataColumn. 此类不能被继承。This class cannot be inherited.

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

示例

下面的示例创建的多个实例OdbcParameter通过OdbcParameterCollectionOdbcDataAdapterThe following example creates multiple instances of OdbcParameter through the OdbcParameterCollection within the OdbcDataAdapter. 这些参数用于从数据源选择数据并在其中放置数据DataSetThese parameters are used to select data from the data source and place the data in the DataSet. 此示例假定DataSet和一个OdbcDataAdapter已创建使用适当的架构、 命令和连接。This example assumes that a DataSet and an OdbcDataAdapter have already been created by using the appropriate schema, commands, and connection.

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

        // Set the parameters.
        adapter.SelectCommand.Parameters.Add(
            "@CategoryName", OdbcType.VarChar, 80).Value = "toasters";
        adapter.SelectCommand.Parameters.Add(
            "@SerialNum", OdbcType.Int).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 OdbcConnection(connectionString)
        Dim adapter As New OdbcDataAdapter(queryString, connection)

        ' Set the parameters.
        adapter.SelectCommand.Parameters.Add( _
            "@CategoryName", OdbcType.VarChar, 80).Value = "toasters"
        adapter.SelectCommand.Parameters.Add( _
         "@SerialNum", OdbcType.Int).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

注解

参数名称不区分大小写。Parameter names are not case sensitive.

在查询时使用 Oracle (MSDAORA) 和 ODBC.NET Framework 数据提供程序的 Microsoft OLE DB 访问接口的 Oracle 数据库,使用LIKE子句对查询中固定长度的字段的值可能不会返回所有预期的匹配项。When querying an Oracle database using the Microsoft OLE DB Provider for Oracle (MSDAORA) and the ODBC .NET Framework Data Provider, using the LIKE clause to query values in fixed-length fields might 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 will 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),其中 Oracle 不会视为"a"的参数值的匹配项的情况下LIKE比较的固定长度的字段。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.

构造函数

OdbcParameter() OdbcParameter() OdbcParameter() OdbcParameter()

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

OdbcParameter(String, Object) OdbcParameter(String, Object) OdbcParameter(String, Object) OdbcParameter(String, Object)

初始化使用参数名称和 OdbcParameter 对象的 OdbcParameter 类的新实例。Initializes a new instance of the OdbcParameter class that uses the parameter name and an OdbcParameter object.

OdbcParameter(String, OdbcType) OdbcParameter(String, OdbcType) OdbcParameter(String, OdbcType) OdbcParameter(String, OdbcType)

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

OdbcParameter(String, OdbcType, Int32) OdbcParameter(String, OdbcType, Int32) OdbcParameter(String, OdbcType, Int32) OdbcParameter(String, OdbcType, Int32)

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

OdbcParameter(String, OdbcType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object) OdbcParameter(String, OdbcType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object) OdbcParameter(String, OdbcType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object) OdbcParameter(String, OdbcType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object)

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

OdbcParameter(String, OdbcType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object) OdbcParameter(String, OdbcType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object) OdbcParameter(String, OdbcType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object) OdbcParameter(String, OdbcType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object)

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

OdbcParameter(String, OdbcType, Int32, String) OdbcParameter(String, OdbcType, Int32, String) OdbcParameter(String, OdbcType, Int32, String) OdbcParameter(String, OdbcType, Int32, String)

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

属性

DbType DbType DbType DbType

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

Direction Direction Direction 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 IsNullable IsNullable IsNullable

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

OdbcType OdbcType OdbcType OdbcType

获取或设置参数的 OdbcTypeGets or sets the OdbcType of the parameter.

Offset Offset Offset Offset
ParameterName ParameterName ParameterName ParameterName

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

Precision Precision Precision Precision

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

Scale Scale Scale Scale

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

Size Size Size Size

获取或设置列内数据的最大大小。Gets or sets the maximum size of the data within the column.

SourceColumn SourceColumn SourceColumn SourceColumn

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

SourceColumnNullMapping SourceColumnNullMapping SourceColumnNullMapping SourceColumnNullMapping

设置或获取一个值,该值指示源列是否可以为 null。Sets or gets a value which indicates whether the source column is nullable. 这使 DbCommandBuilder 能够正确地为可为 null 的列生成 Update 语句。This lets DbCommandBuilder correctly generate Update statements for nullable columns.

SourceVersion SourceVersion SourceVersion SourceVersion

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

Value Value Value Value

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

方法

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

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

(Inherited from MarshalByRefObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
ResetDbType() ResetDbType() ResetDbType() ResetDbType()

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

ResetOdbcType() ResetOdbcType() ResetOdbcType() ResetOdbcType()

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

ToString() ToString() ToString() ToString()

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

显式界面实现

ICloneable.Clone() ICloneable.Clone() ICloneable.Clone() ICloneable.Clone()

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

适用于

另请参阅