在 C# 中获取一张表的架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11169345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:51:15  来源:igfitidea点击:

getting Schema of one Table in C#

c#sql

提问by Saeed

I want to get Schema for a table with name "Petro" in SQL SErver after initializing connectionString, I use this Code

我想在初始化 connectionString 后在 SQL Server 中获取名称为“Petro”的表的架构,我使用此代码

conn.open();
conn.getSchema("Tables");

but it returns schema for all the tables. I only want the Petro schema. What should I do?

但它返回所有表的架构。我只想要 Petro 模式。我该怎么办?

采纳答案by Jason De Oliveira

string[] restrictions = new string[4];
restrictions[2] = "Petro";
DataTable table = conn.GetSchema("Tables",restrictions);

Look here for more information: MSDN: Working with the GetSchema Methods

在此处查看更多信息:MSDN:使用 GetSchema 方法

Edit: use GetSchema instead of getSchema

编辑:使用 GetSchema 而不是 getSchema

回答by Sascha

SQL Server solution:

SQL Server 解决方案:

There is a store procedure called sp_columns. When you run that procedure with the table's name as a parameter, it will return the schema just for that table.

有一个名为sp_columns. 当您使用表的名称作为参数运行该过程时,它将仅返回该表的架构。

回答by Akash KC

You can retrieve the schema in following way:

您可以通过以下方式检索架构:

            string sql = "select * from Petro WHERE 1 = 0";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            DataTable schema = reader.GetSchemaTable();