OdbcParameter 类

定义

表示 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
属性
实现

示例

下面的示例通过 OdbcDataAdapter中的 OdbcParameterCollection 创建多个 OdbcParameter 实例。The 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. 此示例假设已使用适当的架构、命令和连接创建了 DataSetOdbcDataAdapterThis 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 的 Microsoft OLE DB 提供程序(MSDAORA)和 ODBC .NET Framework 数据提供程序查询 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),在 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.

构造函数

OdbcParameter()

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

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 类的新实例。Initializes a new instance of the OdbcParameter class that uses the parameter name and data type.

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 类的新实例,该类使用参数名称、数据类型、长度、源列名、参数方向、数值精度和其他属性。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 类的新实例,该类使用参数名称、数据类型、长度、源列名、参数方向、数值精度和其他属性。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 类的新实例。Initializes a new instance of the OdbcParameter 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.

OdbcType

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

Offset

获取或设置列中数据的偏移量。Gets or sets the offset of the data within the column.

ParameterName

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

Precision

获取或设置用来表示 Value 属性的最大位数。Gets or sets the 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 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 lets DbCommandBuilder 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()

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

ResetOdbcType()

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

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)

适用于

另请参阅