Dela via


SqlParameter Constructors

Definition

Overloads

SqlParameter()

Initializes a new instance of the SqlParameter class.

SqlParameter(String, SqlDbType)

Initializes a new instance of the SqlParameter class that uses the parameter name and the data type.

SqlParameter(String, Object)

Initializes a new instance of the SqlParameter class that uses the parameter name and a value of the new SqlParameter.

SqlParameter(String, SqlDbType, Int32)

Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, and the size.

SqlParameter(String, SqlDbType, Int32, String)

Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, the size, and the source column name.

SqlParameter(String, SqlDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object)

Initializes a new instance of the SqlParameter class that uses the parameter name, the type of the parameter, the size of the parameter, a ParameterDirection, the precision of the parameter, the scale of the parameter, the source column, a DataRowVersion to use, and the value of the parameter.

SqlParameter(String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String)

Initializes a new instance of the SqlParameter class that uses the parameter name, the type of the parameter, the length of the parameter the direction, the precision, the scale, the name of the source column, one of the DataRowVersion values, a Boolean for source column mapping, the value of the SqlParameter, the name of the database where the schema collection for this XML instance is located, the owning relational schema where the schema collection for this XML instance is located, and the name of the schema collection for this parameter.

SqlParameter()

Initializes a new instance of the SqlParameter class.

public:
 SqlParameter();
public SqlParameter ();
Public Sub New ()

Examples

The following example creates a SqlParameter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    private static void AddSqlParameter(SqlCommand command)
    {
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = "@Description";
        parameter.IsNullable = true;
        parameter.SqlDbType = SqlDbType.VarChar;
        parameter.Direction = ParameterDirection.Output;
        parameter.Size = 88;

        command.Parameters.Add(parameter);
    }
}

Applies to

SqlParameter(String, SqlDbType)

Initializes a new instance of the SqlParameter class that uses the parameter name and the data type.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType);
public SqlParameter (string parameterName, System.Data.SqlDbType dbType);
new Microsoft.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType -> Microsoft.Data.SqlClient.SqlParameter
Public Sub New (parameterName As String, dbType As SqlDbType)

Parameters

parameterName
String

The name of the parameter to map.

dbType
SqlDbType

One of the SqlDbType values.

Exceptions

The value supplied in the dbType parameter is an invalid back-end data type.

Examples

The following example creates a SqlParameter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    private static void AddSqlParameter(SqlCommand command, string paramValue)
    {
        SqlParameter parameter = new SqlParameter("@Description", SqlDbType.VarChar);
        parameter.IsNullable = true;
        parameter.Direction = ParameterDirection.Output;
        parameter.Size = 88;
        parameter.Value = paramValue;

        command.Parameters.Add(parameter);
    }
}

Remarks

The data type and, if appropriate, Size and Precision are inferred from the value of the dbType parameter.

Applies to

SqlParameter(String, Object)

Initializes a new instance of the SqlParameter class that uses the parameter name and a value of the new SqlParameter.

public:
 SqlParameter(System::String ^ parameterName, System::Object ^ value);
public SqlParameter (string parameterName, object value);
new Microsoft.Data.SqlClient.SqlParameter : string * obj -> Microsoft.Data.SqlClient.SqlParameter
Public Sub New (parameterName As String, value As Object)

Parameters

parameterName
String

The name of the parameter to map.

value
Object

An Object that is the value of the SqlParameter.

Examples

The following example creates a SqlParameter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    private static void AddSqlParameter(SqlCommand command)
    {
        SqlParameter parameter = new SqlParameter("@Description",
            SqlDbType.VarChar, 88, "Description");
        parameter.IsNullable = true;
        parameter.Direction = ParameterDirection.Output;

        command.Parameters.Add(parameter);
    }
}

Remarks

When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object.

Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.

Parameter = new SqlParameter("@pname", (object)0);  

If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameter(String, SqlDbType) constructor overload.

Applies to

SqlParameter(String, SqlDbType, Int32)

Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, and the size.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType, int size);
public SqlParameter (string parameterName, System.Data.SqlDbType dbType, int size);
new Microsoft.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType * int -> Microsoft.Data.SqlClient.SqlParameter
Public Sub New (parameterName As String, dbType As SqlDbType, size As Integer)

Parameters

parameterName
String

The name of the parameter to map.

dbType
SqlDbType

One of the SqlDbType values.

size
Int32

The length of the parameter.

Exceptions

The value supplied in the dbType parameter is an invalid back-end data type.

Examples

The following example creates a SqlParameter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    private static void AddSqlParameter(SqlCommand command,
        string paramValue)
    {
        SqlParameter parameter = new SqlParameter("@Description",
            SqlDbType.VarChar, 88);
        parameter.IsNullable = true;
        parameter.Direction = ParameterDirection.Output;
        parameter.Value = paramValue;

        command.Parameters.Add(parameter);
    }
}

Remarks

The Size is inferred from the value of the dbType parameter if it is not explicitly set in the size parameter.

Applies to

SqlParameter(String, SqlDbType, Int32, String)

Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, the size, and the source column name.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType, int size, System::String ^ sourceColumn);
public SqlParameter (string parameterName, System.Data.SqlDbType dbType, int size, string sourceColumn);
new Microsoft.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType * int * string -> Microsoft.Data.SqlClient.SqlParameter
Public Sub New (parameterName As String, dbType As SqlDbType, size As Integer, sourceColumn As String)

Parameters

parameterName
String

The name of the parameter to map.

dbType
SqlDbType

One of the SqlDbType values.

size
Int32

The length of the parameter.

sourceColumn
String

The name of the source column (SourceColumn) if this SqlParameter is used in a call to Update(DataSet)>.

Exceptions

The value supplied in the dbType parameter is an invalid back-end data type.

Examples

The following example creates a SqlParameter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    private static void AddSqlParameter(SqlCommand command)
    {
        SqlParameter parameter = new SqlParameter("@Description",
            SqlDbType.VarChar, 88, "Description");
        parameter.IsNullable = true;
        parameter.Direction = ParameterDirection.Output;

        command.Parameters.Add(parameter);
    }
}

Remarks

The Size is inferred from the value of the dbType parameter if it is not explicitly set in the size parameter.

Applies to

SqlParameter(String, SqlDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object)

Initializes a new instance of the SqlParameter class that uses the parameter name, the type of the parameter, the size of the parameter, a ParameterDirection, the precision of the parameter, the scale of the parameter, the source column, a DataRowVersion to use, and the value of the parameter.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType, int size, System::Data::ParameterDirection direction, bool isNullable, System::Byte precision, System::Byte scale, System::String ^ sourceColumn, System::Data::DataRowVersion sourceVersion, System::Object ^ value);
public SqlParameter (string parameterName, System.Data.SqlDbType dbType, int size, System.Data.ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, System.Data.DataRowVersion sourceVersion, object value);
new Microsoft.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType * int * System.Data.ParameterDirection * bool * byte * byte * string * System.Data.DataRowVersion * obj -> Microsoft.Data.SqlClient.SqlParameter
Public Sub New (parameterName As String, dbType As SqlDbType, size As Integer, direction As ParameterDirection, isNullable As Boolean, precision As Byte, scale As Byte, sourceColumn As String, sourceVersion As DataRowVersion, value As Object)

Parameters

parameterName
String

The name of the parameter to map.

dbType
SqlDbType

One of the SqlDbType values.

size
Int32

The length of the parameter.

direction
ParameterDirection

One of the ParameterDirection values.

isNullable
Boolean

true if the value of the field can be null; otherwise, false.

precision
Byte

The total number of digits to the left and right of the decimal point to which Value is resolved.

scale
Byte

The total number of decimal places to which Value is resolved.

sourceColumn
String

The name of the source column (SourceColumn) if this SqlParameter is used in a call to Update(DataSet).

sourceVersion
DataRowVersion

One of the DataRowVersion values.

value
Object

An Object that is the value of the SqlParameter.

Exceptions

The value supplied in the dbType parameter is an invalid back-end data type.

Examples

The following example creates a SqlParameter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    private static void AddSqlParameter(SqlCommand command)
    {
        SqlParameter parameter = new SqlParameter("@Description",
            SqlDbType.VarChar, 11, ParameterDirection.Input,
            true, 0, 0, "Description", DataRowVersion.Current,
            "garden hose");
        parameter.IsNullable = true;

        command.Parameters.Add(parameter);
    }
}

Remarks

The Size and Precision are inferred from the value of the dbType parameter if they are not explicitly set in the size and precision parameters.

Applies to

SqlParameter(String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String)

Initializes a new instance of the SqlParameter class that uses the parameter name, the type of the parameter, the length of the parameter the direction, the precision, the scale, the name of the source column, one of the DataRowVersion values, a Boolean for source column mapping, the value of the SqlParameter, the name of the database where the schema collection for this XML instance is located, the owning relational schema where the schema collection for this XML instance is located, and the name of the schema collection for this parameter.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType, int size, System::Data::ParameterDirection direction, System::Byte precision, System::Byte scale, System::String ^ sourceColumn, System::Data::DataRowVersion sourceVersion, bool sourceColumnNullMapping, System::Object ^ value, System::String ^ xmlSchemaCollectionDatabase, System::String ^ xmlSchemaCollectionOwningSchema, System::String ^ xmlSchemaCollectionName);
public SqlParameter (string parameterName, System.Data.SqlDbType dbType, int size, System.Data.ParameterDirection direction, byte precision, byte scale, string sourceColumn, System.Data.DataRowVersion sourceVersion, bool sourceColumnNullMapping, object value, string xmlSchemaCollectionDatabase, string xmlSchemaCollectionOwningSchema, string xmlSchemaCollectionName);
new Microsoft.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType * int * System.Data.ParameterDirection * byte * byte * string * System.Data.DataRowVersion * bool * obj * string * string * string -> Microsoft.Data.SqlClient.SqlParameter
Public Sub New (parameterName As String, dbType As SqlDbType, size As Integer, direction As ParameterDirection, precision As Byte, scale As Byte, sourceColumn As String, sourceVersion As DataRowVersion, sourceColumnNullMapping As Boolean, value As Object, xmlSchemaCollectionDatabase As String, xmlSchemaCollectionOwningSchema As String, xmlSchemaCollectionName As String)

Parameters

parameterName
String

The name of the parameter to map.

dbType
SqlDbType

One of the SqlDbType values.

size
Int32

The length of the parameter.

direction
ParameterDirection

One of the ParameterDirection values.

precision
Byte

The total number of digits to the left and right of the decimal point to which Value is resolved.

scale
Byte

The total number of decimal places to which Value is resolved.

sourceColumn
String

The name of the source column (SourceColumn) if this SqlParameter is used in a call to Update(DataSet)>.

sourceVersion
DataRowVersion

One of the DataRowVersion values.

sourceColumnNullMapping
Boolean

true if the source column is nullable; false if it is not.

value
Object

An Object that is the value of the SqlParameter.

xmlSchemaCollectionDatabase
String

The name of the database where the schema collection for this XML instance is located.

xmlSchemaCollectionOwningSchema
String

The owning relational schema where the schema collection for this XML instance is located.

xmlSchemaCollectionName
String

The name of the schema collection for this parameter.

Remarks

The Size and Precision are inferred from the value of the dbType parameter if they are not explicitly set in the size and precision parameters.

Applies to