SqlParameter Constructors

Definition

Initializes a new instance of the SqlParameter class.

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()

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlParameter class.

C#
public SqlParameter();

Examples

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

C#
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);
}

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlParameter(String, SqlDbType)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public SqlParameter(string parameterName, System.Data.SqlDbType dbType);

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.

C#
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.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlParameter(String, Object)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public SqlParameter(string parameterName, object value);

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.

C#
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.

C#
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.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlParameter(String, SqlDbType, Int32)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public SqlParameter(string parameterName, System.Data.SqlDbType dbType, int size);

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.

C#
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.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlParameter(String, SqlDbType, Int32, String)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public SqlParameter(string parameterName, System.Data.SqlDbType dbType, int size, string sourceColumn);

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.

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.

C#
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.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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

Source:
System.Data.SqlClient.notsupported.cs

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.

C#
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);

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.

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.

C#
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.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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

Source:
System.Data.SqlClient.notsupported.cs

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.

C#
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);

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.

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.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)