Get column name in C #

I looked at SO on how to get the column name, and I tried some solutions, but when I use this method, for example, I get these column names and not the actual namnes columns (ID, Status, Title, etc.) .):

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

EDIT:

I want to get it in C #. ie do not execute the SQL query string in my code.

EDIT2

Current output:

  • TABLE_CATALOG
  • TABLE_SCHEMA
  • TABLE_NAME
  • TABLE_TYPE

Expected Result:

  • ID
  • Status
  • Title

etc .. column names in tables. string tableNameinstalled correctly.

+4
source share
2 answers

I edited your code and was able to get the tables and columns using 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();
                }
            }


        }
    }
+2
source

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

, , , , . , . 0- ; 1- ; 2- ; 3- .

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

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

, , , , , . , 5 . 0- ; 1- ; 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"]
                     };
+1

Source: https://habr.com/ru/post/1606884/


All Articles