SqlParameter.DbType Property

Definition

Gets or sets the SqlDbType of the parameter.

C#
public override System.Data.DbType DbType { get; set; }

Property Value

One of the SqlDbType values. The default is NVarChar.

Implements

Examples

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

C#
using Microsoft.Data.SqlClient;

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

    private static void SetParameterToNull(IDataParameter parameter)
    {
        if (parameter.IsNullable)
        {
            parameter.Value = DBNull.Value;
        }
        else
        {
            throw new ArgumentException("Parameter provided is not nullable", "parameter");
        }
    }
}

Remarks

The SqlDbType and DbType are linked. Therefore, setting the DbType changes the SqlDbType to a supporting SqlDbType.

For a list of the supported data types, see the appropriate SqlDbType member. For more information, see DataAdapter Parameters.

Applies to

Product Versions
SqlClient .NET Core 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2
SqlClient .NET Framework 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2
SqlClient .NET Standard 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2