// 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;
}

// 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中的数据类型,转换为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;
}

【SqlServer数据类型、C#数据类型、SqlDbType】对应关系及转换的更多相关文章

  1. SqlServer数据类型、C#SqlDbType对应关系及转换

    { { } } { SqlDbType dbType = SqlDbType.Variant; { dbType = SqlDbType.Int; dbType = SqlDbType.VarChar ...

  2. SqlServer中的数据类型UniqueIdentifier

    SqlServer中的数据类型UniqueIdentifier究竟是什么东东? 该类型一般用来做为主键使用,可用SQL语法的newid()来生成一个唯一的值.我想请问的是,这个值是一个长整型的数据值呢 ...

  3. 大数据学习--day02(标识符、变量、数据类型、类型转换、进制转换、原码反码补码)

    标识符.变量.数据类型.类型转换.进制转换.原码反码补码 标识符: java50个关键字不能做标识符,以数字开头不能做标识符(这个老是忘记写一个类名的时候) 变量: 变量分为成员变量和局部变量,注意作 ...

  4. MySql Oracle SqlServer 数据库的数据类型列表

    Oracle数据类型 一.概述  在ORACLE8中定义了:标量(SCALAR).复合(COMPOSITE).引用(REFERENCE)和LOB四种数据类型,下面详细介绍它们的特性. 二.标量(SCA ...

  5. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  6. iOS有哪些数据类型/基本数据类型?

    简述 本文主要探究使用OC作为iOS开发语言时,我们能使用哪些数据类型. 一切类型始于C. C语言的类型 基本数据类型: 基本数据类型(fundamental data types)也叫原始数据类型( ...

  7. Python数据类型-1 数据类型介绍

    数据类型 在python这门语言中,数据类型分为两种. 内置的和自定义的. 内置的包括数字.字符串.布尔.列表.元组.字典.Bytes.集合这些常用的以及一些不太常用的数据类型.而自定义的,一般以类的 ...

  8. SQLServer和java数据类型的对应关系

    转载自:https://www.cnblogs.com/cunkouzh/p/5504052.html SQL Server 类型 JDBC 类型 (java.sql.Types) Java 语言类型 ...

  9. Java基础(34):Java中基本数据类型的包装类(主要为了不同数据类型之间更方便的进行转换)(Wrapper类)

    相信各位小伙伴们对基本数据类型都非常熟悉,例如 int.float.double.boolean.char 等.基本数据类型是不具备对象的特性的,比如基本类型不能调用方法.功能简单...,为了让基本数 ...

随机推荐

  1. VirtualBox 中ubuntu访问window下共享目录

    1,vbox中设置window需要共享给vbox中ubuntu的文件夹,主要要注意共享文件夹名称windowShare. 2,在ubuntu中挂载windowShare. sudo mount -t ...

  2. bootstrap你让前端小狮子们又喜又恨

    看了一个用bootstrap框架建的页面,简直方便至极!各种添加类就可以实现各种功能,各种添加data-toggle让你不需要懂javascript就可以做出很炫的效果! 最重要的它做出来的页面还是响 ...

  3. C# WINFORM 线程中更新UI

    幸好今天是周末,有时间把这个问题记录一下.在多种语言之间切换,发现开发效率降的很低了,开发成本都集中到调式上了,C/C++这些放弃很久了,突然感觉线程这个问题搞的有点烦躁 我这里提到的线程中更新UI, ...

  4. 大家帮我测试下,IOCP服务端和客户端交互

    大家帮我测试下,主要是对游戏服务端的测试,这个客户端C#版本,需要NET4.0支持

  5. (Problem 92)Square digit chains

    A number chain is created by continuously adding the square of the digits in a number to form a new ...

  6. 批处理[Batch]

    批处理 1. 定义:就是一堆DOS命令按一定顺序排列而形成的集合. 英文译为BATCH,批处理文件后缀BAT就取的前三个字母. 示例1:a.bat @echo off Netstat –a –n &g ...

  7. ie7(z-index)

    父级元素加上position:relative;并设置z-index. 父级元素的z-index优先,子元素的z-index是相对于父级元素的index. <div style="po ...

  8. why-and-howto-calculate-your-events-per-second

    http://eromang.zataz.com/2011/04/12/why-and-howto-calculate-your-events-per-second/

  9. Android学习路径图

    一个PHPer转战Android学习过程: 直接跨过java的学习,原因有我之前看过毕向东和张孝祥的Java基础课程,虽然中间好几次看睡着,但java的环境是能跑起来的.我建议大家如果没有Java基础 ...

  10. rsyslogd: error during parsing file /etc/rsyslog.conf, on or before line 55: warnings occured in fil

    zjtest7-frontend:/root# rsyslogd -n rsyslogd: error during parsing file /etc/rsyslog.conf, on or bef ...