如何在运行时在 C# 中获取表的列名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16168395/
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
How to get column names of table at runtime in C#?
提问by
Lets say I have a table in SQLServer named MyTable
假设我在 SQLServer 中有一个名为 MyTable 的表
ID FirstName LastName
1 Harry Dan
2 Maria Vicente
3 Joe Martin
Now if I have to insert any data in table, I will simply fire Insert Query like this
现在,如果我必须在表中插入任何数据,我将简单地像这样触发插入查询
INSERT INTO MyTable (ID, FirstName, LastName) VALUES (4, Smith, Dan);
But what if I don't know the column names beforehand, I only know table name. Then is there a way to get the column name of table at runtime?
但是如果我事先不知道列名,我只知道表名。那么有没有办法在运行时获取表的列名?
采纳答案by Anton Baksheiev
You can use sql-
您可以使用 sql-
SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('TABLE_NAME')
回答by Conrad Clark
Or you can query for SELECT TOP 0 * FROM TableName. Then, you can get the columns:
或者您可以查询SELECT TOP 0 * FROM TableName. 然后,您可以获取列:
using(var reader = command.ExecuteReader())
{
reader.Read();
var table = reader.GetSchemaTable();
foreach (DataColumn column in table.Columns)
{
Console.WriteLine(column.ColumnName);
}
}
回答by Obama
Simply by this Query :
简单地通过这个查询:
SELECT * FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.[table_name]')
OR This Query :
或此查询:
SELECT COLUMN_NAME
FROM information_schema.columns
WHERE TABLE_NAME = [table_name]
ORDER BY COLUMN_NAME
回答by ClotzA
This question was answered before in various threads
这个问题之前在各种线程中得到了回答
check:
查看:
How can I get column names from a table in SQL Server?
How can I get column names from a table in Oracle?
回答by Niels Peter Gibe
Another option using pure C# / .NET code: First a helper method, that here returns a simple list of column names. Using a DataTable to hold table schema information, means that other information can also be retreived for each column, fx. if it is an AutoIncreament column etc.
使用纯 C#/.NET 代码的另一种选择:首先是一个辅助方法,这里返回一个简单的列名列表。使用 DataTable 来保存表模式信息,意味着还可以为每一列检索其他信息,fx。如果它是 AutoIncreament 列等。
private IEnumerable<string> GetColumnNames(string conStr, string tableName)
{
var result = new List<string>();
using (var sqlCon = new SqlConnection(conStr))
{
sqlCon.Open();
var sqlCmd = sqlCon.CreateCommand();
sqlCmd.CommandText = "select * from " + tableName + " where 1=0"; // No data wanted, only schema
sqlCmd.CommandType = CommandType.Text;
var sqlDR = sqlCmd.ExecuteReader();
var dataTable = sqlDR.GetSchemaTable();
foreach (DataRow row in dataTable.Rows) result.Add(row.Field<string>("ColumnName"));
}
return result;
}
The method can be called as:
该方法可以调用为:
var sortedNames = GetColumnNames("Data Source=localhost;Initial Catalog=OF2E;Integrated Security=SSPI", "Articles").OrderBy(x => x);
foreach (var columnName in sortedNames) Console.WriteLine(columnName);
回答by kandarp rami
var ColName = "";
var model = new AttributeMappingSource().GetModel(typeof(DataClassesDataContext));
foreach (var mt in model.GetTables())
{
if (mt.TableName == "dbo.Table_Name")
{
foreach (var dm in mt.RowType.DataMembers)
{
ColName = dm.MappedName + ", ";
Response.Write(ColName);
}
}
}

