【发布时间】:2022-01-08 06:03:56
【问题描述】:
我有一个组合框,可以在其中显示数据库中所有表的名称。现在我找不到通过在组合框中选择表名来在数据网格视图中显示表的所有内容的方法。目前我有 2 个表,我可以通过在组合框中选择它并在数据网格视图中显示它的值来进行选择。
我想要实现的是,通过在组合框中选择表名,我的数据网格视图将根据组合框中选择的表名显示表的值
这是我的代码:
private void samples_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conn);
try
{
con.Open();
MySqlCommand sqlCmd = new MySqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "select table_name from information_schema.tables where table_schema = 'attenddb'";
MySqlDataAdapter sqlDataAdap = new MySqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
comboBox1.DataSource = dtRecord;
comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string query2 = "select table_name from ['"+ comboBox1.Text + "']";
MySqlConnection con2 = new MySqlConnection(conn);
MySqlCommand com2 = new MySqlCommand(query2, con2);
MySqlDataAdapter myadapt = new MySqlDataAdapter();
myadapt.SelectCommand = com2;
DataTable dtable = new DataTable();
myadapt.Fill(dtable);
dataGridView1.DataSource = dtable;
}
catch
{
MessageBox.Show("Error Loading data");
}
}
【问题讨论】:
-
您好 Forgamez,您的具体问题是什么? :) 添加这可能会更容易回答您的问题。
-
您可能还想阅读 SQL 注入:bobby-tables.com
-
我编辑了,抱歉没有解释????
标签: c# mysql visual-studio phpmyadmin