Get column name from SQL Server

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

    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();
            }
    
    0 讨论(0)
提交回复
热议问题