在C#中检索列名

时间:2015-09-10 23:11:34

标签: c# sql-server ado.net

我正在寻找关于如何检索列名的SO,我尝试了一些解决方案,但是当我使用这种方法时,我收到这些列名而不是我的实际列namnes(ID,Status,Title等) 。):

  • TABLE_CATALOG
  • TABLE_SCHEMA
  • TABLE_NAME
  • 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);
            }
        }
    

修改

我想在C#中检索它,即不在我的代码中执行SQL查询字符串。

EDIT2

当前输出:

  • TABLE_CATALOG
  • TABLE_SCHEMA
  • TABLE_NAME
  • TABLE_TYPE

预期产出:

  • ID
  • 状态
  • 标题

等。表中的列名。 string tableName设置正确。

2 个答案:

答案 0 :(得分:2)

我编辑了您的代码,并能够使用下面的代码获取表格和列。

   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();
                }
            }


        }
    }

答案 1 :(得分:1)

获取当前数据库中所有列的架构信息

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

您可以指定目录,架构,表名称,列名称以获取指定的列。 您可以对Column使用四个限制,因此您应该创建一个4成员数组。 对于数组,0-member表示Catalog; 1名成员代表Schema; 2名成员代表表名; 3个成员代表列名。

e.g。获取表MyTable的列:

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

从这些表中获取数据:

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"]
                         };

获取当前数据库中所有IndexColumns的架构信息

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

您可以指定目录,架构,表名称,约束名称,列名称以获取指定的列。 您可以对Column使用五个限制,因此您应该创建一个5成员数组。 对于数组,0-member表示Catalog; 1名成员代表Schema; 2名成员代表表名; 3人代表Constraint Name; 4个成员代表列名。

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

从这些表中获取数据:

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"]
                     };