OdbcParameter.Size 属性


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

 virtual property int Size { int get(); void set(int value); };
 property int Size { int get(); void set(int value); };
public override int Size { get; set; }
public int Size { get; set; }
member this.Size : int with get, set
Public Overrides Property Size As Integer
Public Property Size As Integer


列内数据的最大大小。The maximum size of the data within the column. 默认值将根据参数值进行推断。The default value is inferred from the parameter value.



下面的示例创建一个 OdbcParameter 并设置其一些属性。The following example creates an OdbcParameter and sets some of its properties.

Public Sub CreateOdbcParameter()  
    Dim myValue As String = "12 foot scarf - multiple colors, one previous owner"  
    Dim parameter As New OdbcParameter("Description", OdbcType.VarChar)  
    parameter.Direction = ParameterDirection.Output  
    parameter.Size = myValue.Length  
    parameter.Value = myValue  
End Sub   
public void CreateOdbcParameter()   
    string myValue = "12 foot scarf - multiple colors, one previous owner";  
    OdbcParameter parameter = new OdbcParameter("Description", OdbcType.VarChar);  
    parameter.Direction = ParameterDirection.Output;  
    parameter.Size = myValue.Length;  
    parameter.Value = myValue;  


Size 属性用于二进制和字符串类型。The Size property is used for binary and string types.

对于非字符串数据类型和 ANSI 字符串数据,Size 属性是指字节数。For nonstring data types and ANSI string data, the Size property refers to the number of bytes. 对于 Unicode 字符串数据,Size 是指字符数。For Unicode string data, Size refers to the number of characters. 字符串的计数不包括终止字符。The count for strings does not include the terminating character.

对于可变长度数据类型,Size 描述要传输到服务器的最大数据量。For variable-length data types, Size describes the maximum amount of data to transmit to the server. 例如,对于 Unicode 字符串值,可以使用 Size 将发送到服务器的数据量限制为前100个字符。For example, for a Unicode string value, Size could be used to limit the amount of data sent to the server to the first one hundred characters.

对于双向参数、输出参数和返回值,必须设置 Size的值。For bidirectional and output parameters, and return values, you must set the value of Size. 这不是输入参数所必需的,如果未显式设置,则执行参数化语句时,的值将从指定参数的实际大小推断。This is not required for input parameters, and if not explicitly set, the value of is inferred from the actual size of the specified parameter when a parameterized statement is executed.

可以通过设置 Value来推断参数的 DbTypeOdbcTypeSize 属性。The DbType, OdbcType, and Size properties of a parameter can be inferred by setting Value. 因此,您不需要指定它们。Therefore, you are not required to specify them. 但是,它们不 OdbcParameter 属性设置公开。However, they are not exposed in OdbcParameter property settings. 例如,如果已推断出参数的大小,则在语句执行后 Size 不包含推断值。For example, if the size of the parameter has been inferred, Size does not contain inferred value after statement execution.


对于 Direction ParameterDirection.OutputParameterDirection.InputOutputParameterDirection.ReturnValueOdbcParameter,如果数据源返回的值的大小超过 Size 的指定 OdbcParameter,则在执行命令时,将引发异常。For an OdbcParameter with a Direction of ParameterDirection.Output, ParameterDirection.InputOutput, or ParameterDirection.ReturnValue, an exception is thrown when the command is executed if the size of the value returned by the data source exceeds the specified Size of the OdbcParameter.

对于固定长度的数据类型,将忽略 Size 的值。For fixed-length data types, the value of Size is ignored. 可以检索它以提供信息,并返回将参数值传输到服务器时提供程序使用的最大字节数。It can be retrieved for informational purposes, and returns the maximum amount of bytes the provider uses when transmitting the value of the parameter to the server.