我可以在列表框中找到并显示所有表的所有名称。从C#中,如何查找SQL Server中表的列名称?
但我需要通过单击按钮来显示列表框中选定表的列名称。
我的功能:在按键
public void GetTableNames()
{
string strConnect = "Data Source=;Initial Catalog=DATA;User ID=sa;Password=***";
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand(@"SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY TABLE_NAME ASC ", con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
listBox2.Items.Clear();
int counter = 0;
while (reader.Read())
{
counter++;
listBox2.Items.Add((string)reader["TABLE_NAME"]);
}
lblTablesCount.Text = counter.ToString();
}
}
}
}
呼叫功能
private void button1_Click(object sender, EventArgs e)
{
GetTableNames();
}
我的问题:给定一个选择的表,我怎么能找到该表的列的名称?用户从列表框中选取表格并单击一个按钮。
['INFORMATION_SCHEMA.COLUMNS'](http://technet.microsoft.com/en-us/library/ms188348.aspx)也许?或者['sys.objects'](http://technet.microsoft.com/en-us/library/ms190324.aspx)。 –
'select column_name from information_schema.columns' 'where table_name = @ TableName' 'order by ordinal_position' – CJBS