Get column name from SQL Server

后端 未结 7 2228
礼貌的吻别
礼貌的吻别 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:26

    public string[] getColumnsName()
        {
            List listacolumnas=new List();
            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();
        }
    

提交回复
热议问题