C# 通过 GetSchema() 方法获取表的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11160685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Columns of a Table by GetSchema() method
提问by Navid Farhadi
I want to get list of columns of a table using GetSchemamethod in ADO.Net, my code is:
我想使用GetSchemain 方法获取表的列列表ADO.Net,我的代码是:
var dtCols = con.GetSchema("Columns", new[] { "DBName", "TableName" });
And i get an empty DataTable, what is the problem?
我得到一个空的DataTable,有什么问题?
采纳答案by Matthew
You must specify a parameter for the "owner" restriction.
您必须为“所有者”限制指定一个参数。
var dtCols = con.GetSchema("Columns", new[] { "DBName", null, "TableName" });
回答by Philip Sobolik
Could both of these answers be generalized a bit with:
这两个答案是否可以概括为:
dtCols = con.GetSchema("Columns", new[] {con.DataSource, null, "TableName"});
dtCols = con.GetSchema("Columns", new[] {con.DataSource, null, "TableName"});
This is assuming that "TableName" is the name of the table that you want the schema for.
这是假设“TableName”是您想要架构的表的名称。
回答by Dimitris M
I had a similar problem, the following worked..
我有一个类似的问题,以下工作..
using(SqlCommand command = new SqlCommand(sqlText, con)) {
var sqlReader = command.ExecuteReader();
var a = sqlReader.GetColumnSchema();
}

