// 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. nginx+php的配置

    作了N多次php环境的搭建,网上的方法还真是多,但是实际操作起来总有一些大大小小的出入,很多错误经常让我纠结不已.久病成医,渐渐地我自己就总结出了一些经验.自我感觉良好. 这种方法并非以前所流行的ap ...

  2. postman下载和安装

    插件下载地址:http://download.csdn.net/download/zhanghaofor/8244137 下载后解压缩,里面有安装方法 1.找到后缀为crx的文件,将后缀改成rar并解 ...

  3. codeforces 149E . Martian Strings kmp

    题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...

  4. 无法打开 configsource 文件

    右键点击*.config文件,属性里的“复制到输出目录”选项,选择“始终复制”或“如果较新则复制”,这样生成或运行时,该文件就会出现在bin目录或obj目录中.

  5. php正则表达式的基本语法

    简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例 如,vi编辑器,Perl或PHP脚本语言,以及awk或sed sh ...

  6. iOS判断机型

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_OPTIONS(NSInteger,D ...

  7. Android Layout Binder(在线将XML中View find出来,生成java代码的工具)

    废话不多说,这是地址:http://android.lineten.net/layout.php. 有图有真相,比如: 你的XML假如是这样: <?xml version="1.0&q ...

  8. android技术牛人的博客[转]

    Android+JNI调用–文件操作 开发环境:Windows xp sp3 +MyEclipse 8.6+android2.3.3+jdk1.6+android-ndk-r6b JNI概述:     ...

  9. OpenVPN多处理之-多队列TUN多线程

    1.有一点不正确劲 在改动了那个TUN驱动后,我在想,为何我总是对一些驱动程序进行修修补补而从来不从应用程序找解决方式呢?我改动了那个TUN驱动,可是能保证我的改动对别的应用一样可用吗?难道TUN驱动 ...

  10. 纯代码 自己主动屏幕适配iPhone button

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2h1bmdlc2hpaHVhdGlhbg==/font/5a6L5L2T/fontsize/400/fil ...