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

php中文网
发布: 2016-06-07 15:27:01
原创
2890人浏览过

// sql server数据类型(如:varchar) // 转换为SqlDbType类型 public static SqlDbType SqlTypeString2SqlType(string sqlTypeString) { SqlDbType dbType = SqlDbType.Variant;//默认为Object switch (sqlTypeString) { case "int": dbType = SqlDbType.I

// 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;
}
}
代码:

魔法映像企业网站管理系统
魔法映像企业网站管理系统

技术上面应用了三层结构,AJAX框架,URL重写等基础的开发。并用了动软的代码生成器及数据访问类,加进了一些自己用到的小功能,算是整理了一些自己的操作类。系统设计上面说不出用什么模式,大体设计是后台分两级分类,设置好一级之后,再设置二级并选择栏目类型,如内容,列表,上传文件,新窗口等。这样就可以生成无限多个二级分类,也就是网站栏目。对于扩展性来说,如果有新的需求可以直接加一个栏目类型并新加功能操作

魔法映像企业网站管理系统 0
查看详情 魔法映像企业网站管理系统


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

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号