如何获取数据表中列的SqlType? [英] How to get the SqlType of a column in a DataTable?

查看:254
本文介绍了如何获取数据表中列的SqlType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从SQL数据库获得的数据表,如下所示:

I have a DataTable obtained from a SQL DataBase, like this:

using (SqlCommand cmd = new SqlCommand(query, _sqlserverDB))
{
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
    {
        DataSet dataSet = new DataSet();
        adapter.Fill(dataSet);
        result = (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count > 0) ? dataSet.Tables[0] : null;
    }
}

当我尝试通过获取每个列的DataType时dataColumn.DataType,我得到C#类型(Int32,Int64,String等)。

When I try to get the DataType of each column through dataColumn.DataType , I get the C# types (Int32, Int64, String, etc).

问题:如何访问本机SQL数据类型(varchar,nvarchar,bigint ...)而不是C#类型?

QUESTION: How can I access the native SQL data types (varchar, nvarchar, bigint...) instead of the C# types?

我尝试了dataColumn.DataType.UnderlyingSystemType,结果是相同的。

I have tried dataColumn.DataType.UnderlyingSystemType and the result is the same.

推荐答案

您不能因为 System.Data.DataTable (或 DataColumn DataSet DataRow

You cannot because System.Data.DataTable (or DataColumn, or DataSet, or DataRow...) is a generic .NET data container which works the same way regardless on the specific database engine you loaded your data from.

这意味着您为SQL Server,MySQL使用了.NET连接器,Access,PostgreSQL或其他任何东西, DataTable DataColumn 类始终相同,并且作为ADO.NET对象是通用,可与任何数据库一起使用gine,因此您可以使用发现的.NET类型来键入列。

this means that provided you used a .NET Connector for SQL Server, MySQL, Access, PostgreSQL or anything else, the DataTable and DataColumn classes are always the same and being ADO.NET objects are generic to work with any db engine, so the columns are typed with the .NET types as you have found out.

这篇关于如何获取数据表中列的SqlType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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