datagrid视图

m1m5dgzv  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(322)

我认为问题出在适配器上。不过,我还是很生疏,如果有任何帮助我都会很感激的。我搜索过这个论坛,但没有找到任何我找到的解决方案(可能是pebkac)。应用程序很简单,在参数化select语句中使用textbox输入来填充datagridview。查询将执行,但网格仍为空。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);
            }

        }
xriantvc

xriantvc1#

尝试不带参数的查询,不需要这样做,然后尝试数据集的datasourceview。

nwo49xxi

nwo49xxi2#

乍一看,您的查询可能不好。尝试在列名周围使用反勾号。

SELECT * FROM part_data WHERE `SERIAL` = @test;

相关问题