Chỉnh sửa

SqlParameter Constructors

Definition

Initializes a new instance of the SqlParameter class.

Overloads

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

Source:
System.Data.SqlClient.notsupported.cs

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.

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);
}
Private Sub AddSqlParameter(ByVal command As SqlCommand)

    Dim parameter As New SqlParameter()
    With parameter
        .ParameterName = "@Description"
        .IsNullable = True
        .SqlDbType = SqlDbType.VarChar
        .Direction = ParameterDirection.Output
        .Size = 88
    End With

    command.Parameters.Add(parameter)
End Sub

See also

Applies to

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.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType);
public SqlParameter(string parameterName, System.Data.SqlDbType dbType);
new System.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType -> System.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.

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);
}
Private Sub AddSqlParameter(ByVal command As SqlCommand, _
    ByVal paramValue As String)

    Dim parameter As New SqlParameter("@Description", _
        SqlDbType.VarChar)
    With parameter
        .IsNullable = True
        .Direction = ParameterDirection.Output
        .Size = 88
        .Value = paramValue
    End With

    command.Parameters.Add(parameter)
End Sub

Remarks

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

See also

Applies to

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.

public:
 SqlParameter(System::String ^ parameterName, System::Object ^ value);
public SqlParameter(string parameterName, object value);
new System.Data.SqlClient.SqlParameter : string * obj -> System.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.

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);
}
Private Sub AddSqlParameter(ByVal command As SqlCommand)

    Dim parameter As New SqlParameter("@Description", _
        SqlDbType.VarChar, 88, "Description")
    With parameter
        .IsNullable = True
        .Direction = ParameterDirection.Output
    End With

    command.Parameters.Add(parameter)
End Sub

Remarks

When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET 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.

See also

Applies to

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.

public:
 SqlParameter(System::String ^ parameterName, System::Data::SqlDbType dbType, int size);
public SqlParameter(string parameterName, System.Data.SqlDbType dbType, int size);
new System.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType * int -> System.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.

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);
}
Private Sub AddSqlParameter(ByVal command As SqlCommand, _
    ByVal paramValue As String)

    Dim parameter As New SqlParameter("@Description", _
        SqlDbType.VarChar, 88)
    With parameter
        .IsNullable = True
        .Direction = ParameterDirection.Output
        .Value = paramValue
    End With

    command.Parameters.Add(parameter)
End Sub

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

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.

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 System.Data.SqlClient.SqlParameter : string * System.Data.SqlDbType * int * string -> System.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.

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.

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);
}
Private Sub AddSqlParameter(ByVal command As SqlCommand)

    Dim parameter As New SqlParameter("@Description", _
        SqlDbType.VarChar, 88, "Description")
    With parameter
        .IsNullable = True
        .Direction = ParameterDirection.Output
    End With

    command.Parameters.Add(parameter)
End Sub

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