SQL Server数据类型与C#类型对应关系

     发布时间:2022-04-01 20:49:46

SQL Server数据类型与C#类型对应关系

一、 SQL Server与C#类型映射

SQL Server类型 C#类型
bit bool
tinyint byte
smallint short
int int
bigint long
real float
float double
money decimal
datetime DateTime
char string
varchar string
nchar string
nvarchar string
text string
ntext string
image byte[]
binary byte[]
uniqueidentifier Guid

二、 类型转换代码

/// <summary>
/// SqlDbType转C#类型
/// </summary>
/// <param name="type">SQL类型</param>
/// <returns>C#类型</returns>
public static Type SqlTypeToCsharpType(SqlDbType type)
{
    switch (type)
    {
        case SqlDbType.BigInt:
            return typeof(long);
        case SqlDbType.Binary:
            return typeof(object);
        case SqlDbType.Bit:
            return typeof(bool);
        case SqlDbType.Char:
            return typeof(string);
        case SqlDbType.DateTime:
            return typeof(DateTime);
        case SqlDbType.Decimal:
            return typeof(decimal);
        case SqlDbType.Float:
            return typeof(double);
        case SqlDbType.Image:
            return typeof(object);
        case SqlDbType.Int:
            return typeof(int);
        case SqlDbType.Money:
            return typeof(decimal);
        case SqlDbType.NChar:
            return typeof(string);
        case SqlDbType.NText:
            return typeof(string);
        case SqlDbType.NVarChar:
            return typeof(string);
        case SqlDbType.Real:
            return typeof(float);
        case SqlDbType.SmallDateTime:
            return typeof(DateTime);
        case SqlDbType.SmallInt:
            return typeof(short);
        case SqlDbType.SmallMoney:
            return typeof(decimal);
        case SqlDbType.Text:
            return typeof(string);
        case SqlDbType.Timestamp:
            return typeof(object);
        case SqlDbType.TinyInt:
            return typeof(byte);
        case SqlDbType.Udt:
            return typeof(object);
        case SqlDbType.UniqueIdentifier:
            return typeof(object);
        case SqlDbType.VarBinary:
            return typeof(object);
        case SqlDbType.VarChar:
            return typeof(string);
        case SqlDbType.Variant:
            return typeof(object);
        case SqlDbType.Xml:
            return typeof(object);
        default:
            return null;
    }
}

/// <summary>
/// SqlServer类型字符串转SqlDbType枚举
/// </summary>
/// <param name="sqlTypeString">SqlServer类型字符串</param>
/// <returns>SqlDbType枚举</returns>
public static SqlDbType SqlTypeStringToSqlType(string sqlTypeString)
{
    if ("sql_variant".Equals(sqlTypeString, StringComparison.CurrentCultureIgnoreCase))
    {
        return SqlDbType.Variant;
    }

    SqlDbType type;
    return Enum.TryParse(sqlTypeString, true, out type)
        ? type
        : SqlDbType.Variant;
}

/// <summary>
/// SqlServer类型字符串转C#类型
/// </summary>
/// <param name="sqlTypeString">SqlServer类型字符串</param>
/// <returns>C#类型</returns>
public static Type SqlTypeStringToCsharpType(string sqlTypeString)
{
    SqlDbType sqlDbType = SqlTypeStringToSqlType(sqlTypeString);
    return SqlTypeToCsharpType(sqlDbType);
}