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


     1 // SqlDbType转换为C#数据类型
    2 public static Type SqlType2CsharpType(SqlDbType sqlType)
    3 {
    4 switch (sqlType)
    5 {
    6 case SqlDbType.BigInt:
    7 return typeof(Int64);
    8 case SqlDbType.Binary:
    9 return typeof(Object);
    10 case SqlDbType.Bit:
    11 return typeof(Boolean);
    12 case SqlDbType.Char:
    13 return typeof(String);
    14 case SqlDbType.DateTime:
    15 return typeof(DateTime);
    16 case SqlDbType.Decimal:
    17 return typeof(Decimal);
    18 case SqlDbType.Float:
    19 return typeof(Double);
    20 case SqlDbType.Image:
    21 return typeof(Object);
    22 case SqlDbType.Int:
    23 return typeof(Int32);
    24 case SqlDbType.Money:
    25 return typeof(Decimal);
    26 case SqlDbType.NChar:
    27 return typeof(String);
    28 case SqlDbType.NText:
    29 return typeof(String);
    30 case SqlDbType.NVarChar:
    31 return typeof(String);
    32 case SqlDbType.Real:
    33 return typeof(Single);
    34 case SqlDbType.SmallDateTime:
    35 return typeof(DateTime);
    36 case SqlDbType.SmallInt:
    37 return typeof(Int16);
    38 case SqlDbType.SmallMoney:
    39 return typeof(Decimal);
    40 case SqlDbType.Text:
    41 return typeof(String);
    42 case SqlDbType.Timestamp:
    43 return typeof(Object);
    44 case SqlDbType.TinyInt:
    45 return typeof(Byte);
    46 case SqlDbType.Udt://自定义的数据类型
    47 return typeof(Object);
    48 case SqlDbType.UniqueIdentifier:
    49 return typeof(Object);
    50 case SqlDbType.VarBinary:
    51 return typeof(Object);
    52 case SqlDbType.VarChar:
    53 return typeof(String);
    54 case SqlDbType.Variant:
    55 return typeof(Object);
    56 case SqlDbType.Xml:
    57 return typeof(Object);
    58 default:
    59 return null;
    60 }
    61 }
     1 // sql server数据类型(如:varchar)
    2 // 转换为SqlDbType类型
    3 public static SqlDbType SqlTypeString2SqlType(string sqlTypeString)
    4 {
    5 SqlDbType dbType = SqlDbType.Variant;//默认为Object
    6
    7 switch (sqlTypeString)
    8 {
    9 case "int":
    10 dbType = SqlDbType.Int;
    11 break;
    12 case "varchar":
    13 dbType = SqlDbType.VarChar;
    14 break;
    15 case "bit":
    16 dbType = SqlDbType.Bit;
    17 break;
    18 case "datetime":
    19 dbType = SqlDbType.DateTime;
    20 break;
    21 case "decimal":
    22 dbType = SqlDbType.Decimal;
    23 break;
    24 case "float":
    25 dbType = SqlDbType.Float;
    26 break;
    27 case "image":
    28 dbType = SqlDbType.Image;
    29 break;
    30 case "money":
    31 dbType = SqlDbType.Money;
    32 break;
    33 case "ntext":
    34 dbType = SqlDbType.NText;
    35 break;
    36 case "nvarchar":
    37 dbType = SqlDbType.NVarChar;
    38 break;
    39 case "smalldatetime":
    40 dbType = SqlDbType.SmallDateTime;
    41 break;
    42 case "smallint":
    43 dbType = SqlDbType.SmallInt;
    44 break;
    45 case "text":
    46 dbType = SqlDbType.Text;
    47 break;
    48 case "bigint":
    49 dbType = SqlDbType.BigInt;
    50 break;
    51 case "binary":
    52 dbType = SqlDbType.Binary;
    53 break;
    54 case "char":
    55 dbType = SqlDbType.Char;
    56 break;
    57 case "nchar":
    58 dbType = SqlDbType.NChar;
    59 break;
    60 case "numeric":
    61 dbType = SqlDbType.Decimal;
    62 break;
    63 case "real":
    64 dbType = SqlDbType.Real;
    65 break;
    66 case "smallmoney":
    67 dbType = SqlDbType.SmallMoney;
    68 break;
    69 case "sql_variant":
    70 dbType = SqlDbType.Variant;
    71 break;
    72 case "timestamp":
    73 dbType = SqlDbType.Timestamp;
    74 break;
    75 case "tinyint":
    76 dbType = SqlDbType.TinyInt;
    77 break;
    78 case "uniqueidentifier":
    79 dbType = SqlDbType.UniqueIdentifier;
    80 break;
    81 case "varbinary":
    82 dbType = SqlDbType.VarBinary;
    83 break;
    84 case "xml":
    85 dbType = SqlDbType.Xml;
    86 break;
    87 }
    88 return dbType;
    89 }
     1 // sql server中的数据类型,转换为C#中的类型类型
    2 public static Type SqlTypeString2CsharpType(string sqlTypeString)
    3 {
    4 SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);
    5
    6 return SqlType2CsharpType(dbTpe);
    7 }
    8
    9 // 将sql server中的数据类型,转化为C#中的类型的字符串
    10 public static string SqlTypeString2CsharpTypeString(string sqlTypeString)
    11 {
    12 Type type = SqlTypeString2CsharpType(sqlTypeString);
    13
    14 return type.Name;
    15 }






    原文链接:http://www.cnblogs.com/Relict/archive/2011/12/19/2293460.html

  • 相关阅读:
    MsSql数据库存储过程加密解密
    Delete Exists
    SQL Server 2008数据库日志收缩
    MSSQL PIVOT 实现行列转置
    Oracle中将查询出的多条记录的某个字段拼接成一个字符串的方法
    oracle的分析函数over 及开窗函数
    Asprise-OCR的使用
    【整理】动态加载Web Services
    【转载】Python正则表达式指南
    Mac中PyCharm设置GitHub的步骤
  • 原文地址:https://www.cnblogs.com/Percy_Lee/p/5069751.html
Copyright © 2020-2023  润新知