什么是错误的mySQL查询在我的应用程序

本文关键字:我的 应用程序 查询 mySQL 错误 什么 | 更新日期: 2023-09-27 18:11:32

我有一个主表单(formMain),它在其加载事件中加载用户控件(分类)。在用户控件分类的load事件中,它显示一个datagridview。让我给你看代码。

string serverstring = "user id = root; password=; server=localhost; database=purchase_order; connection timeout=3;";
        private void load_data() 
        {
            MySqlConnection con = new MySqlConnection(serverstring);
            try
            {
                string query = "SELECT * FROM tblclassification";
                MySqlCommand cmd = new MySqlCommand(query, con);
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);

                DataTable dt = new DataTable();
                da.Fill(dt);

                dataGridView1.DataSource = dt;
                dataGridView1.DataMember = dt.TableName;
            }
            catch (Exception)
            {

            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }    
        }

幸运的是,它的工作,但当我点击搜索按钮位于另一个用户控件(搜索)它引发了一个事件在主表单(formMain),其中它必须过滤在用户控件(分类)的datagridview,但我的代码不工作。也许我有一个语法错误在我的字符串查询。代码如下:

主要形式

void SearchClicked(object sender, EventArgs e)
        {
            Search content = _searchbox;
            classification control = new classification();
            MySqlConnection con = new MySqlConnection(serverstring);

            try
            {                
                string query = "SELECT * FROM tblclassification WHERE class_name LIKE '%@search'";
                MySqlCommand cmd = new MySqlCommand(query, con);
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);

                //MessageBox.Show(content.txtboxSearch.Text);
                cmd.Parameters.AddWithValue("@search", content.txtboxSearch.Text);

                DataTable dt = new DataTable();
                da.Fill(dt);


                control.dataGridView1.DataSource = dt;
                control.dataGridView1.DataMember = dt.TableName;

            }
            catch (Exception)
            {
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }    
        }

什么是错误的mySQL查询在我的应用程序

use CONCAT()

string query = @"SELECT * 
                 FROM   tblclassification 
                 WHERE  class_name LIKE CONCAT('%', @search)";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@search", content.txtboxSearch.Text);

MySqlDataAdapter da = new MySqlDataAdapter(cmd);

try

"SELECT * FROM purchase_order. "tblclassification WHERE class_name LIKE ('%@search%')";

或. .

"SELECT * FROM tblclassification WHERE (class_name LIKE '%@search%')"