SqlServer数据类型、C#SqlDbType对应关系及转换
2015-12-23 13:54
483 查看
// 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; }
原文链接:http://www.cnblogs.com/Relict/archive/2011/12/19/2293460.html
相关文章推荐
- ORACLEHELPER.cs文件,比较全,好用
- SQL Server高速生成SQL增删改查语句
- mysqldump AUTO_INCREMENT
- 关于存储过程和游标的总结
- SQL-行转列(PIVOT)实例1
- Re:李捷_NoSQL数据库之Redis数据库管理五(Redis的常用命令及高级应用)
- Mongodb
- discuz mysql 错误 (1045) notconnect
- Mysql 查看定时器 打开定时器 设置定时器时间
- SQL Tuning 基础概述09 - SQL Access Advisor
- mysql 修改root密码
- sql server 的cpu使用率过高的分析
- MySQL数据库操作常用脚本
- Linux下MySQL/MariaDB Galera集群搭建过程
- cmd下操作mongodb
- 数据库系统原理
- ORA-01033 : ORACLE initialization or shutdown in progress
- Oracle集合操作函数:Union、Union All、Intersect、Minus
- 【PostgreSQL】PostgreSQL常用备份方法
- sqllite3命令行下的常用命令及接口