Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column name from SQL Server

I'm trying to get the column names of a table I have stored in SQL Server 2008 R2.

I've literally tried everything but I can't seem to find how to do this.

Right now this is my code in C#

public string[] getColumnsName()
{
        List<string> listacolumnas=new List<string>();

        using (SqlConnection connection = new SqlConnection(Connection))
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = "SELECT TOP 0 * FROM Usuarios";
            connection.Open();

            using (var reader = command.ExecuteReader(CommandBehavior.KeyInfo))
            {
                reader.Read();

                var table = reader.GetSchemaTable();

                foreach (DataColumn column in table.Columns)
                {
                    listacolumnas.Add(column.ColumnName);
                }
            }
        }
        return listacolumnas.ToArray();
    }

But this is returning me the following

<string>ColumnName</string>
<string>ColumnOrdinal</string>
<string>ColumnSize</string>
<string>NumericPrecision</string>
<string>NumericScale</string>
<string>IsUnique</string>
<string>IsKey</string>
<string>BaseServerName</string>
<string>BaseCatalogName</string>
<string>BaseColumnName</string>
<string>BaseSchemaName</string>
<string>BaseTableName</string>
<string>DataType</string>
<string>AllowDBNull</string>
<string>ProviderType</string>
<string>IsAliased</string>
<string>IsExpression</string>
<string>IsIdentity</string>
<string>IsAutoIncrement</string>
<string>IsRowVersion</string>
<string>IsHidden</string>
<string>IsLong</string>
<string>IsReadOnly</string>
<string>ProviderSpecificDataType</string>
<string>DataTypeName</string>
<string>XmlSchemaCollectionDatabase</string>
<string>XmlSchemaCollectionOwningSchema</string>
<string>XmlSchemaCollectionName</string>
<string>UdtAssemblyQualifiedName</string>
<string>NonVersionedProviderType</string>
<string>IsColumnSet</string>

Any ideas?

It shows the <string> tags as this is how my web service sends the data.

like image 822
Cristian Eduardo Lehuede Lyon Avatar asked Oct 15 '13 22:10

Cristian Eduardo Lehuede Lyon


People also ask

How do I get column names in SQL?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.

How do I get a list of column names in SQL Server?

Column search A feature that can be used to search for column names in SQL Server is Object search. This feature allows users to find all SQL objects containing the specified phrase.

How can I get column details of a table in SQL Server?

The System Stored Procedure sp_columns returns Column names with additional information from the Database table in SQL Server. In the following example, the Column names of Employees table of Northwind Database are fetched using the Stored Procedure sp_columns.

How do I get the column names of a table?

We can verify the data in the table using the SELECT query as below. We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information.


1 Answers

I typically use the GetSchema method to retrieve Column specific information, this snippet will return the column names in a string List:

        using (SqlConnection conn = new SqlConnection("<ConnectionString>"))
        {
            string[] restrictions = new string[4] { null, null, "<TableName>", null };
            conn.Open();
            var columnList = conn.GetSchema("Columns", restrictions).AsEnumerable().Select(s => s.Field<String>("Column_Name")).ToList();
        }
like image 84
Mark Kram Avatar answered Sep 23 '22 19:09

Mark Kram