查询以获取列名 [英] query for retrieving column name
本文介绍了查询以获取列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想用数据库中的表的列名加载选中的列表框.
任何人都可以通过sqlquery帮助我,以仅检索表中的列名吗?
在此先感谢.
Hi All,
I want to load my checked listbox with column name of a table from Database.
Can anyone please help me with the sqlquery to retrieve only the column name in the table?
Thanks in Advance.
推荐答案
尝试此
try this
SELECT [name] AS [Column name]
FROM syscolumns
WHERE id = (SELECT id
FROM sysobjects
WHERE type = 'U'
AND [NAME] = 'Your Table Name ')
您可以使用以下查询表获取列列表.
you can use following queyr to get the list of columns.
select * from information_schema.columns where table_name = ''customer''
最好采用这种方法,因为信息架构视图提供了SQL Server元数据的内部,独立于系统表的视图.
您可以在这里阅读更多内容
信息Shema视图 [
its best to take this approach since Information schema views provide an internal, system table-independent view of the SQL Server metadata.
you can read more here
Information shema views[^]
我想这会为您提供帮助:
I suppose this will help you:
using System;
using System.Data;
using System.Data.SqlClient;
class SchemaTable
{
static void Main(string[] args)
{
string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql = @"select * from employee";
SqlConnection conn = new SqlConnection(connString);
try {
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
DataTable schema = reader.GetSchemaTable();
foreach (DataRow row in schema.Rows)
{
foreach (DataColumn col in schema.Columns){
Console.WriteLine(col.ColumnName + " = " + row[col]);
Console.WriteLine("Null value allowed: " + col.AllowDBNull);
}
}
reader.Close();
} catch(Exception e) {
Console.WriteLine("Error Occurred: " + e);
} finally {
conn.Close();
}
}
}
这篇关于查询以获取列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文