Get column name from SQL Server

后端 未结 7 2227
礼貌的吻别
礼貌的吻别 2021-02-14 05:32

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.

Ri

相关标签:
7条回答
  • 2021-02-14 06:07

    You can use the query below to get the column names for your table. The query below gets all the columns for a user table of a given name:

    select c.name from sys.columns c
    inner join sys.tables t 
    on t.object_id = c.object_id
    and t.name = 'Usuarios' and t.type = 'U'
    

    In your code, it will look like that:

    public string[] getColumnsName()
    {
        List<string> listacolumnas=new List<string>();
        using (SqlConnection connection = new SqlConnection(Connection))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "select c.name from sys.columns c inner join sys.tables t on t.object_id = c.object_id and t.name = 'Usuarios' and t.type = 'U'";
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        listacolumnas.Add(reader.GetString(0));
                    }
                }
            }
        return listacolumnas.ToArray();
    }
    
    0 讨论(0)
  • 2021-02-14 06:07

    Currently, there are two ways I could think of doing this:

    • In pure SQL Server SQL you can use the views defined in INFORMATION_SCHEMA.COLUMNS. There, you would need to select the row for your table, matching on the column TABLE_NAME.
    • Since you are using C#, it's probably easier to obtain the names from the SqlDataReader instance that is returned by ExecuteReader. The class provides a property FieldCount, for the number of columns, and a method GetName(int), taking the column number as its argument and returning the name of the column.
    0 讨论(0)
  • 2021-02-14 06:16
    sp_columns - http://technet.microsoft.com/en-us/library/ms176077.aspx
    

    There are many built in stored procedures for this type of thing.

    0 讨论(0)
  • 2021-02-14 06:18

    The original post was close to the goal, Just some small changes and you got it. Here is my solution.

       public List<string> GetColumns(string tableName)
        {
            List<string> colList = new List<string>();
            DataTable dataTable = new DataTable();
    
    
            string cmdString = String.Format("SELECT TOP 0 * FROM {0}", tableName);
    
            if (ConnectionManager != null)
            {
                try
                {
                    using (SqlDataAdapter dataContent = new SqlDataAdapter(cmdString, ConnectionManager.ConnectionToSQL))
                    {
                        dataContent.Fill(dataTable);
    
                        foreach (DataColumn col in dataTable.Columns)
                        {
                           colList.Add(col.ColumnName);
                        }
                    }                   
                }
                catch (Exception ex)
                {
                    InternalError = ex.Message;
                }
            }
    
    
            return colList;
        }
    
    0 讨论(0)
  • 2021-02-14 06:24
    SELECT COLUMN_NAME
    FROM   
    INFORMATION_SCHEMA.COLUMNS 
    WHERE   
    TABLE_NAME = 'YourTable' 
    
    0 讨论(0)
  • 2021-02-14 06:26
    public string[] getColumnsName()
        {
            List<string> listacolumnas=new List<string>();
            using (SqlConnection connection = new SqlConnection(Connection))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "select column_name from information_schema.columns where table_name = '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();
        }
    
    0 讨论(0)
提交回复
热议问题