将DataColumn.DataType转换为SqlDbType [英] Convert DataColumn.DataType to SqlDbType

查看:286
本文介绍了将DataColumn.DataType转换为SqlDbType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在用于从DataColumn.DataType转换为SqlDbType的转换器?还是我必须写一个方法来做到这一点?

Is there a converter for going from DataColumn.DataType to SqlDbType? Or do I have to write a method to do it?

推荐答案

我在点网脉冲 CodeProject 。我最终将CodeProject代码从VB.NET转换为C#:

I found a couple of options at Dot Net Pulse and CodeProject. I eventually went with the CodeProject code converted from VB.NET to C#:

private SqlDbType GetDBType(System.Type theType)
{
     System.Data.SqlClient.SqlParameter p1;
     System.ComponentModel.TypeConverter tc;
     p1 = new System.Data.SqlClient.SqlParameter();
     tc = System.ComponentModel.TypeDescriptor.GetConverter(p1.DbType);
     if (tc.CanConvertFrom(theType)) {
        p1.DbType = (DbType)tc.ConvertFrom(theType.Name);
     } else {
        //Try brute force
        try {
           p1.DbType = (DbType)tc.ConvertFrom(theType.Name);
        }
        catch (Exception) {
           //Do Nothing; will return NVarChar as default
        }
     }
     return p1.SqlDbType;
}

我真的很希望发现有一些隐藏的秘密System.Data。 SqlClient方法执行转换,我只是想念它。

I was really hoping to find that there was some hidden secret System.Data.SqlClient method to do the conversion and I was just missing it.

这篇关于将DataColumn.DataType转换为SqlDbType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆