随笔 - 432  文章 - 0  评论 - 15  阅读 - 63万

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

/ SqlDbType转换为C#数据类型

复制代码
public static Type SqlType2CsharpType(SqlDbType sqlType)
{
    switch (sqlType)
    {
        case SqlDbType.BigInt:
            return typeof(Int64);
        case SqlDbType.Binary:
            return typeof(Object);
        case SqlDbType.Bit:
            return typeof(Boolean);
        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(Int32);
        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(Single);
        case SqlDbType.SmallDateTime:
            return typeof(DateTime);
        case SqlDbType.SmallInt:
            return typeof(Int16);
        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;
    }
}
复制代码

 

复制代码
// sql server数据类型(如:varchar)
// 转换为SqlDbType类型
public static SqlDbType SqlTypeString2SqlType(string sqlTypeString)
{
    SqlDbType dbType = SqlDbType.Variant;//默认为Object

    switch (sqlTypeString)
    {
        case "int":
            dbType = SqlDbType.Int;
            break;
        case "varchar":
            dbType = SqlDbType.VarChar;
            break;
        case "bit":
            dbType = SqlDbType.Bit;
            break;
        case "datetime":
            dbType = SqlDbType.DateTime;
            break;
        case "decimal":
            dbType = SqlDbType.Decimal;
            break;
        case "float":
            dbType = SqlDbType.Float;
            break;
        case "image":
            dbType = SqlDbType.Image;
            break;
        case "money":
            dbType = SqlDbType.Money;
            break;
        case "ntext":
            dbType = SqlDbType.NText;
            break;
        case "nvarchar":
            dbType = SqlDbType.NVarChar;
            break;
        case "smalldatetime":
            dbType = SqlDbType.SmallDateTime;
            break;
        case "smallint":
            dbType = SqlDbType.SmallInt;
            break;
        case "text":
            dbType = SqlDbType.Text;
            break;
        case "bigint":
            dbType = SqlDbType.BigInt;
            break;
        case "binary":
            dbType = SqlDbType.Binary;
            break;
        case "char":
            dbType = SqlDbType.Char;
            break;
        case "nchar":
            dbType = SqlDbType.NChar;
            break;
        case "numeric":
            dbType = SqlDbType.Decimal;
            break;
        case "real":
            dbType = SqlDbType.Real;
            break;
        case "smallmoney":
            dbType = SqlDbType.SmallMoney;
            break;
        case "sql_variant":
            dbType = SqlDbType.Variant;
            break;
        case "timestamp":
            dbType = SqlDbType.Timestamp;
            break;
        case "tinyint":
            dbType = SqlDbType.TinyInt;
            break;
        case "uniqueidentifier":
            dbType = SqlDbType.UniqueIdentifier;
            break;
        case "varbinary":
            dbType = SqlDbType.VarBinary;
            break;
        case "xml":
            dbType = SqlDbType.Xml;
            break;
    }
    return dbType;
}
复制代码
复制代码
// sql server中的数据类型,转换为C#中的类型类型
public static Type SqlTypeString2CsharpType(string sqlTypeString)
{
    SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);

    return SqlType2CsharpType(dbTpe);
}

// 将sql server中的数据类型,转化为C#中的类型的字符串
public static string SqlTypeString2CsharpTypeString(string sqlTypeString)
{
    Type type = SqlTypeString2CsharpType(sqlTypeString);

    return type.Name;
}
复制代码

 

posted on   狼来了  阅读(484)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示