I can find and display all names of all tables in a listbox.
But I need display column names of selected table from listbox by click a button.
My function :
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();
}
}
}
}
Call function in button
private void button1_Click(object sender, EventArgs e)
{
GetTableNames();
}
My question : Given a selected table, how can I find the names of the columns of that table? The user picks the table from a listbox and clicks a button.