0

我使用以下代码在组合框中显示我的 SQL 数据库表的名称。

现在我希望当我从组合框中单击任何这些表名时,我的 DGV 会填充该表的内容。

private void Form1_Load(object sender, EventArgs e)
{
    String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

    SqlConnection con = new SqlConnection(strConnection);
    try
    {
        con.Open();

        SqlCommand sqlCmd = new SqlCommand();

        sqlCmd.Connection = con;
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "Select table_name from information_schema.tables";

        SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

        DataTable dtRecord = new DataTable();
        sqlDataAdap.Fill(dtRecord);
        comboBox1.DataSource = dtRecord;
        comboBox1.DisplayMember = "TABLE_NAME";
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

然后我使用以下代码填充我的 DGV,但它不起作用;请帮忙。

private void PopulateGridView()
{
    String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
    SqlConnection con = new SqlConnection(strconnection);

    try
    {
        con.Open();

        SqlCommand sqlCmd = new SqlCommand();

        sqlCmd.Connection = con;
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "select * from " + comboBox1.SelectedText;

        SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

        DataTable dtRecord = new DataTable();
        sqlDataAdap.Fill(dtRecord);
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = dtRecord;
        dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedValue != null)
    {
        PopulateGridView(comboBox1.SelectedValue.ToString());
    }
}
4

2 回答 2

1

试试这个:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedValue != null)
        {
           string strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
            SqlConnection con = new SqlConnection(strconnection);
            try
            {
                con.Open();
                SqlCommand sqlCmd = new SqlCommand();

                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = "select * from " + comboBox1.SelectedValue;
                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = dtRecord;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
于 2013-03-13T06:53:56.203 回答
1

试试这个:在表单加载中更新您的代码,如下所示:

 private void Form1_Load(object sender, EventArgs e)
 {
    String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
    SqlConnection con = new SqlConnection(strConnection);
    try
    {
        con.Open();
        SqlCommand sqlCmd = new SqlCommand();

        sqlCmd.Connection = con;
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "Select table_name from information_schema.tables";

        SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
        DataTable dtRecord = new DataTable();
        sqlDataAdap.Fill(dtRecord);
        comboBox1.DataSource = dtRecord;
        comboBox1.DisplayMember = "table_name";
        comboBox1.ValueMember = "table_name";
        con.Close();
   }
   catch (Exception ex)
   {
        MessageBox.Show(ex.Message);
   }
}

所以基本上你已经多加了一行 comboBox1.ValueMember = "table_name";

并使您的 PopulateGridView 方法如下所示:

   private void PopulateGridView(string tblName)
        {
            String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
            SqlConnection con = new SqlConnection(strConnection);

            try
            {
                con.Open();

                SqlCommand sqlCmd = new SqlCommand();

                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = "select * from " + tblName;

                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = dtRecord;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

它必须工作。另外,我知道,您正在SqlConnection到处创建对象,包括SqlCommandand SqlDataAdapter

尝试将它们包装在静态方法中,即

 - public static SqlConnection OpenConnection()
 - public static DataTable ExecuteSelectQuery(string Query)
 - public static bool ExecuteModifyQuery(string Query)

尽量少写代码。

于 2013-03-13T07:09:48.600 回答