惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
罗磊的独立博客
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX

博客园 - 一叶浮萍

Visual Studio 2022 Net6.0 无法发现testcase, 也无法执行test case Microsoft.AspNetCore.Http.Abstractions 2.20 is deprecated 使用office365 world2016发布编辑备份你的博客 使用office365 world2016发布编辑备份你的博客 You are not late! You are not early! 在同一个服务器(同一个IP)为不同域名绑定的免费SSL证书 Vue.js Is Good, but Is It Better Than Angular or React? It was not possible to find any compatible framework version VS增加插件 Supercharger破解教程 Git使用ssh key Disconnected: No supported authentication methods available (server sent: publickey) VS 2013打开.edmx文件时报类型转换异常 Echarts ecomfe 触摸屏 touch 在IE10下无法显示悬浮框 64位系统里注册32位软件 Bonobo Git Server (Simple git server for Windows.) 测试备忘 TortoiseGit bonobo gitserver记住帐号密码 TortoiseGit bonobo gitserver记住帐号密码 TITLE: BizTalk Server 2013 Administration Console 应用程序-特定 权限设置并未向在应用程序容器 不可用 SID (不可用)中运行的地址 LocalHost (使用 LRPC) 中的用户
System.Data.Dbtype转换为System.Data.SqlDbType
一叶浮萍 · 2014-06-26 · via 博客园 - 一叶浮萍

最近在做一些OM Mapping的准备工作,新学了一招。

如果要将System.Data.Dbtype转换为System.Data.SqlDbType,以前以为要写Switch Case语句。其实有很简单的方法:

        private System.Data.SqlDbType ConvertToSqlDbType(System.Data.DbType pSourceType)
        {
            SqlParameter paraConver = new SqlParameter();
            paraConver.DbType = pSourceType ;
            return paraConver.SqlDbType;
        }

微软为你做好了转换。在SqlParameter 中你改变了其中一个就自动改变了另外一个。

你也可以做一个自动生成mapping的方法,你想怎么映射就怎么映射,同理也可以对应oracle的类型

        [TestMethod]

public void BuildDbTypeToSqlDbType()

{

    var dbTypeFields =

    Enum.GetValues(typeof(DbType));

    foreach (DbType dbTypeField in dbTypeFields)

    {

        SqlParameter p = new SqlParameter();

        try

        {

            p.DbType = dbTypeField;

            Console.WriteLine("DbType." + dbTypeField.ToString() + " =\t SqlDbType." + p.SqlDbType);

        }

        catch (Exception)

        {

            Console.WriteLine("// DbType." + dbTypeField.ToString() + " =\t unknown");

        }

    }

}

[TestMethod]

public void BuildSqlDbTypeToDbType()

{

    var dbTypeFields =

    Enum.GetValues(typeof(SqlDbType));

    foreach (SqlDbType sqldbtype in dbTypeFields)

    {

        SqlParameter p = new SqlParameter();

        try

        {

            p.SqlDbType = sqldbtype;

            Console.WriteLine("SqlDbType." + sqldbtype + " =\t DbType." + p.DbType);

        }

        catch (Exception)

        {

            Console.WriteLine("// SqlDbType." + sqldbtype.ToString() + " =\t unknown");

        }

    }

}