我如何使用ADO.NET中的一个表中的列的SqlDbType? [英] How do I get the SqlDbType of a column in a table using ADO.NET?
问题描述
我想在运行时确定哪一个SQL Server表列的SqlDbType为。
I'm trying to determine at runtime what the SqlDbType of a sql server table column is.
有一个类,能做到这一点在System.Data.SqlClient的或者我应该做的映射自己?我可以得到一个字符串再presentation回程从
is there a class that can do that in System.Data.SqlClient or should I do the mapping myself? I can get a string representation back from
SELECT DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = '{0}' AND TABLE_SCHEMA = '{1}'
AND TABLE_NAME = '{2}' AND COLUMN_NAME = '{3}'
编辑:我不能使用SMO,因为我有过执行机器无法控制,所以我不能保证它会被安装。 (对不起,不使这一明确的RP)。
I can't use SMO as I have no control over the executing machine so I can't guarantee it will be installed. (Sorry for not making that clear rp).
编辑:在回答乔尔,我试图做一个功能,我可以打电话的时候通过一个SqlConnection,表名,将返回我的SqlDbType和列名
In answer to Joel, I'm trying to make a function that I can call that will return me a SqlDBType when passed a SqlConnection, a table name, and a column name.
推荐答案
在SQL Server中,你可以使用FMTONLY选项。它可以让你没有得到任何数据,只返回列运行查询。
In SQL Server you can use the FMTONLY option. It allows you to run a query without getting any data, just returning the columns.
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SET FMTONLY ON; select column from table; SET FMTONLY OFF";
SqlDataReader reader = cmd.ExecuteReader();
SqlDbType type = (SqlDbType)(int)reader.GetSchemaTable().Rows[0]["ProviderType"];
这篇关于我如何使用ADO.NET中的一个表中的列的SqlDbType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!