-1

我的想法是问题出在适配器上。但是,我仍然很年轻,希望能得到任何帮助。我搜索了这个论坛,但我找到的任何解决方案都没有运气(可能是pebkac)。该应用程序很简单,在参数化选择语句中使用文本框输入来填充数据网格视图。查询将执行,但网格保持为空。VS2017,MySql8.0

private void button2_Click(object sender, EventArgs e)
    {
        string myConnectionstring = null;
        string mySelect = "SELECT * FROM part_data WHERE 'SERIAL' = @test; ";
        string tb7 = textBox7.Text;
        //Set Connection String And Create Connection
        myConnectionstring = "server=localhost;user= admin; database= trace;port=3306;password= admin;Allow User Variables=True";

        DataSet ds = new DataSet();

        using (MySqlConnection myConnection = new MySqlConnection(myConnectionstring))
        {   //Create Command Object
            //MySqlCommand myCommand = new MySqlCommand(mySelect, myConnection);



            try
            {
                myConnection.Open();
               // myCommand.Prepare();
                //myCommand.Parameters.AddWithValue("@test",tb7);
                MySqlDataAdapter adapter = new MySqlDataAdapter(mySelect, myConnectionstring);
                adapter.SelectCommand.Parameters.AddWithValue("@test",tb7);
                //debug to see final select statement
                richTextBox1.Text = mySelect;


                adapter.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            { 
                MessageBox.Show("Query Failed" + ex);
            }


        }
4

2 回答 2

0

乍一看,您的查询似乎很糟糕。尝试在列名周围使用反引号。

SELECT * FROM part_data WHERE `SERIAL` = @test; 
于 2018-10-09T21:07:40.207 回答
0

尝试不带参数进行查询,不需要那样做,然后尝试数据集的DataSourceView。

于 2018-10-09T21:11:08.100 回答