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

推荐订阅源

D
DataBreaches.Net
罗磊的独立博客
雷峰网
雷峰网
量子位
V
Visual Studio Blog
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
月光博客
月光博客
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
腾讯CDC
Engineering at Meta
Engineering at Meta
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
Jina AI
Jina AI
A
About on SuperTechFans

博客园 - 一叶浮萍

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");

        }

    }

}