简体   繁体   English

在C#中检索列名

[英]Retrieve Column name in C#

I was looking around on SO on how to retrieve the column name and I tried out some solutions but when I use this method, for example, I recieve these column names and not my actual column namnes (ID, Status, Title etc.): 我正在寻找关于如何检索列名的SO,我尝试了一些解决方案但是当我使用这种方法时,我收到这些列名而不是我的实际列名(ID,Status,Title等):

  • TABLE_CATALOG TABLE_CATALOG
  • TABLE_SCHEMA TABLE_SCHEMA
  • TABLE_NAME TABLE_NAME
  • TABLE_TYPE TABLE_TYPE

      using (SqlConnection connection = new SqlConnection(this.ConnectionString)) { System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.ConnectionString = this.ConnectionString; string server = builder.DataSource; string database = builder.InitialCatalog; connection.Open(); DataTable schema = connection.GetSchema("Tables"); Tables = new List<Table>(); foreach (DataRow row in schema.Rows) { /* Add Table */ Table t = new Table(); string tableName = row[2].ToString(); t.Name = tableName; /* Add columns */ //DataTable dtCols = connection.GetSchema("Columns", new[] { "StarTrackerDB", null, "dbo.Tickets" }); t.Columns = new List<Column>(); foreach (DataColumn column in row.Table.Columns) { Column c = new Column(); c.Name = column.ColumnName; t.Columns.Add(c); } Tables.Add(t); } } 

EDIT: 编辑:

I want to retrieve it in C# ie not execute an SQL query string in my code. 我想在C#中检索它,即不在我的代码中执行SQL查询字符串。

EDIT2 EDIT2

Current output: 当前输出:

  • TABLE_CATALOG TABLE_CATALOG
  • TABLE_SCHEMA TABLE_SCHEMA
  • TABLE_NAME TABLE_NAME
  • TABLE_TYPE TABLE_TYPE

Expected output: 预期产量:

  • ID ID
  • Status 状态
  • Title 标题

etc. the column names in the tables. 等等表中的列名。 the string tableName is set correctly. string tableName设置正确。

I edited your code and was able to get the tables and columns with the code below. 我编辑了您的代码,并能够使用下面的代码获取表格和列。

   public void testeStackOverflow()
    {
        using (SqlConnection connection = new SqlConnection(this.ConnectionString))
        {

            System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
            builder.ConnectionString = this.ConnectionString;
            string server = builder.DataSource;
            string database = builder.InitialCatalog;

            connection.Open();


            DataTable schemaTables = connection.GetSchema("Tables");

            foreach (System.Data.DataRow rowTable in schemaTables.Rows)
            {
                String TableName = rowTable.ItemArray[2].ToString();

                string[] restrictionsColumns = new string[4];
                restrictionsColumns[2] = TableName;
                DataTable schemaColumns = connection.GetSchema("Columns", restrictionsColumns);

                foreach (System.Data.DataRow rowColumn in schemaColumns.Rows)
                {
                    string ColumnName = rowColumn[3].ToString();
                }
            }


        }
    }

Get schema information of all the columns in current database 获取当前数据库中所有列的架构信息

DataTable allColumnsSchemaTable = connection.GetSchema("Columns");

You can specify the Catalog, Schema, Table Name, Column Name to get the specified column(s). 您可以指定目录,架构,表名称,列名称以获取指定的列。 You can use four restrictions for Column, so you should create a 4 members array. 您可以对Column使用四个限制,因此您应该创建一个4成员数组。 For the array, 0-member represents Catalog; 对于数组,0-member表示Catalog; 1-member represents Schema; 1名成员代表Schema; 2-member represents Table Name; 2名成员代表表名; 3-member represents Column Name. 3个成员代表列名。

eg get columns for table MyTable: 例如获取表MyTable的列:

String[] columnRestrictions = new String[4];
columnRestrictions[2] = "MyTable";
DataTable myTableSchemaTable = connection.GetSchema("Columns", columnRestrictions);

To get the data from these tables: 要从这些表中获取数据:

var columnDetails = from info in table.AsEnumerable()
                         select new {
                            TableCatalog = info["TABLE_CATALOG"],
                            TableSchema = info["TABLE_SCHEMA"],
                            TableName = info["TABLE_NAME"],
                            ColumnName = info["COLUMN_NAME"],
                            DataType = info["DATA_TYPE"]
                         };

Get schema information of all the IndexColumns in current database 获取当前数据库中所有IndexColumns的架构信息

DataTable allIndexColumnsSchemaTable = connection.GetSchema("IndexColumns");

You can specify the Catalog, Schema, Table Name, Constraint Name, Column Name to get the specified column(s). 您可以指定目录,架构,表名称,约束名称,列名称以获取指定的列。 You can use five restrictions for Column, so you should create a 5 members array. 您可以对Column使用五个限制,因此您应该创建一个5成员数组。 For the array, 0-member represents Catalog; 对于数组,0-member表示Catalog; 1-member represents Schema; 1名成员代表Schema; 2-member represents Table Name; 2名成员代表表名; 3-member represents Constraint Name; 3人代表Constraint Name; 4-member represents Column Name. 4个成员代表列名。

String[] indexColumnsRestrictions = new String[5];
indexColumnsRestrictions[2] = "Course";
indexColumnsRestrictions[4] = "CourseID";
DataTable courseIdIndexSchemaTable = connection.GetSchema("IndexColumns", indexColumnsRestrictions);

To get the data from these tables: 要从这些表中获取数据:

var columnDetails = from info in indexColumnsTable.AsEnumerable()
                     select new {
                        TableSchema = info["table_schema"],
                        TableName = info["table_name"],
                        ColumnName = info["column_name"],
                        ConstraintSchema = info["constraint_schema"],
                        ConstraintName = info["constraint_name"],
                        KeyType = info["KeyType"]
                     };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM