架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6456|回复: 0

[资料] SQL Server类型与C#类型对应关系

[复制链接]
发表于 2016-1-19 11:04:06 | 显示全部楼层 |阅读模式
QQ截图20160119110326.jpg

SQL Server类型C#类型
bitbool
tinyintbyte
smallintshort
intint
bigintlong
realfloat
floatdouble
moneydecimal
datetimeDateTime
charstring
varcharstring
ncharstring
nvarcharstring
textstring
ntextstring
imagebyte[]
binarybyte[]
uniqueidentifierGuid/ 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;
}






上一篇:SQL Server 中return_value返回值的含义
下一篇:.net将DataTable转成集合
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-5-5 07:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表