将SQLServer数据类型转换为.Net中SqlDbType类型
作者:C/S原创  发布日期:2011/03/17 22:50:27
  将SQLServer数据类型转换为.Net中SqlDbType类型

将SQLServer数据类型转换为.Net中SqlDbType类型


SQLServer数据类型转换为.Net中SqlDbType类型(版本1)


/// <summary>
/// 将SQLServer数据类型名称(如:varchar)转换为.Net中SqlDbType类型(如:SqlDbType.VarChar)
/// </summary>
/// <param name="sqlTypeString">SQLServer数据类型名称</param>
/// <returns></returns>
public static SqlDbType SqlTypeName2SqlType(string sqlTypeString)
{
   string[] SqlTypeNames = new string[] { "int", "varchar","bit" ,"datetime","decimal","float","image","money",
   "ntext","nvarchar","smalldatetime","smallint","text","bigint","binary","char","nchar","numeric",
   "real","smallmoney", "sql_variant","timestamp","tinyint","uniqueidentifier","varbinary","xml"};
   
   SqlDbType[] SqlTypes = new SqlDbType[] {SqlDbType.Int,SqlDbType.VarChar,SqlDbType.Bit ,SqlDbType.DateTime,SqlDbType.Decimal,SqlDbType.Float,SqlDbType.Image,SqlDbType.Money,
   SqlDbType.NText,SqlDbType.NVarChar,SqlDbType.SmallDateTime,SqlDbType.SmallInt,SqlDbType.Text,SqlDbType.BigInt,SqlDbType.Binary,SqlDbType.Char,SqlDbType.NChar,SqlDbType.Decimal,
   SqlDbType.Real,SqlDbType.SmallMoney, SqlDbType.Variant,SqlDbType.Timestamp,SqlDbType.TinyInt,SqlDbType.UniqueIdentifier,SqlDbType.VarBinary,SqlDbType.Xml};
   
   int i = Array.IndexOf(SqlTypeNames, sqlTypeString.ToLower());
   
   return SqlTypes[i];
}

// 来源:www.CSFramework.com, C/S结构框架学习网



SQLServer数据类型转换为.Net中SqlDbType类型(版本2)


C# Code:

/// <summary>
/// MsSql数据类型简称对应.NET类型 - MsSql数据类型(SqlDbType)名称对应.NET类型(Type)
/// </summary>
public static IDictionary<string, Type> SqlDbTypeName2NetTypeMapping
{
  
get
  {
    IDictionary
<String, Type> map = new Dictionary<String, Type>();
    map.Add(
"BigInt", typeof(System.Int64));
    map.Add(
"Binary", typeof(System.Byte[]));
    map.Add(
"Bit", typeof(System.Boolean));
    map.Add(
"Char", typeof(System.Char));
    map.Add(
"Cursor", typeof(System.Object));//前端不用
    
     map.Add("Date", typeof(System.DateTime));
    map.Add(
"DateTime", typeof(System.DateTime));
    map.Add(
"DateTime2", typeof(System.DateTime));
    map.Add(
"DateTimeOffset", typeof(System.DateTimeOffset));
    map.Add(
"Decimal", typeof(System.Decimal));
    map.Add(
"Float", typeof(System.Double));
    map.Add(
"Image", typeof(System.Byte[]));
    map.Add(
"Int", typeof(System.Int32));
    map.Add(
"Money", typeof(System.Decimal));
    map.Add(
"NChar", typeof(System.String));
    map.Add(
"NText", typeof(System.String));
    map.Add(
"NVarChar", typeof(System.String));
    map.Add(
"Real", typeof(System.Double));//float类型,double
    
     map.Add("SmallDateTime", typeof(System.DateTime));
    map.Add(
"SmallInt", typeof(System.Int16));
    map.Add(
"SmallMoney", typeof(System.Decimal));
    map.Add(
"sql_variant", typeof(System.Object));//sql_variant
    
     map.Add("Structured", typeof(System.Object));//前端不用
    
     map.Add("Text", typeof(System.String));
    map.Add(
"Time", typeof(System.DateTime));
    map.Add(
"Timestamp", typeof(System.DateTime));//byte[], 可以定义为datetime
    
     map.Add("TinyInt", typeof(System.Int16));
    map.Add(
"Udt", typeof(System.Object));//前端不用,UDT是用户自定义数据类型
    
     map.Add("UniqueIdentifier", typeof(System.Guid));
    map.Add(
"VarBinary", typeof(System.Byte[]));
    map.Add(
"VarChar", typeof(System.String));
    map.Add(
"Variant", typeof(System.Object));//sql_variant
    
     map.Add("Xml", typeof(System.String));
    
    
return map;
  }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761




C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

上一篇 下一篇