Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MYSQL-搜索使用组合框和文本框过滤datagridview_C#_Mysql_Datagridview_Combobox_Textbox - Fatal编程技术网

C#MYSQL-搜索使用组合框和文本框过滤datagridview

C#MYSQL-搜索使用组合框和文本框过滤datagridview,c#,mysql,datagridview,combobox,textbox,C#,Mysql,Datagridview,Combobox,Textbox,您好,我正在尝试使用组合框和文本框搜索过滤器datagridview 我已经成功地这样做了,但它只有在搜索ID列时才能正常工作。其他列仅显示以下消息: 您的SQL语法有错误;检查手册 对应于要使用的正确语法的MariaDB服务器版本 在第1行的“名称”附近,类似于“d%” 该错误消息中的d字母正是我试图过滤搜索的字母 有人能帮我解决这个问题吗 我的代码在下面 string myConnection = "datasource=localhost;port=3306;username=root;p

您好,我正在尝试使用组合框和文本框搜索过滤器
datagridview

我已经成功地这样做了,但它只有在搜索ID列时才能正常工作。其他列仅显示以下消息:

您的SQL语法有错误;检查手册 对应于要使用的正确语法的MariaDB服务器版本 在第1行的“名称”附近,类似于“d%”

该错误消息中的d字母正是我试图过滤搜索的字母

有人能帮我解决这个问题吗

我的代码在下面

string myConnection = "datasource=localhost;port=3306;username=root;password=;";
MySqlConnection conDatabase = new MySqlConnection(myConnection);
try
        {
            if (comboBoxSrchPatient.Text == "ID")
            {
                MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE ID LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmd;

                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                dataPatientGridView.DataSource = dbdataset;
            }
            else if (comboBoxSrchPatient.Text == "FIRST NAME")
            {
                MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE First Name LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmd;

                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                dataPatientGridView.DataSource = dbdataset;
            }

            else if (comboBoxSrchPatient.Text == "LAST NAME")
            {
                MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE Last Name LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmd;

                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                dataPatientGridView.DataSource = dbdataset;
            }

            else if (comboBoxSrchPatient.Text == "AGE")
            {
                MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE Age LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmd;

                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                dataPatientGridView.DataSource = dbdataset;
            }

            else if (comboBoxSrchPatient.Text == "CONTACT NUMBER")
            {
                MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE Contact Number LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmd;

                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                dataPatientGridView.DataSource = dbdataset;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

您的字段名包含空格。
要在查询中使用它们,需要将它们括在反勾号之间(ALT+096)

表示,请尽快考虑更改查询以使用参数化查询

using(MySqlCommand cmd = new MySqlCommand(@"select * from 
            clinic_inventory_system.patient 
            WHERE `First Name` LIKE @name", conDatabase);
{
    cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtSearchPatient.Text + "%";
    MySqlDataAdapter sda = new MySqlDataAdapter();
    sda.SelectCommand = cmd;
    DataTable dbdataset = new DataTable();
    sda.Fill(dbdataset);
    dataPatientGridView.DataSource = dbdataset;
}

通过这种方式,您的代码更安全,因为不再可能针对数据库构建Sql注入攻击,并且如果
名字包含一个引号,则不会再次出现语法错误您的字段名包含空格。
要在查询中使用它们,需要将它们括在反勾号之间(ALT+096)

表示,请尽快考虑更改查询以使用参数化查询

using(MySqlCommand cmd = new MySqlCommand(@"select * from 
            clinic_inventory_system.patient 
            WHERE `First Name` LIKE @name", conDatabase);
{
    cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtSearchPatient.Text + "%";
    MySqlDataAdapter sda = new MySqlDataAdapter();
    sda.SelectCommand = cmd;
    DataTable dbdataset = new DataTable();
    sda.Fill(dbdataset);
    dataPatientGridView.DataSource = dbdataset;
}

通过这种方式,您的代码更加安全,因为不再可能针对您的数据库构建Sql注入攻击,并且如果
名字
包含一个引号,您就不会再次出现语法错误

首先,对于名字、姓氏和联系人号码,您需要正确地转义列。 因为您使用的是MariaDB,所以应该使用backticks(`)来转义列名

其次,年龄查询失败,因为无法对数字列执行LIKE。您应该使用=(等于)

希望有帮助


此外,如果您使用的是用户直接在SQL中提供的数据,请考虑切换到预处理语句。目前,您对SQL注入持开放态度。

首先,对于名字、姓氏和联系电话,您需要正确地转义这些列。 因为您使用的是MariaDB,所以应该使用backticks(`)来转义列名

其次,年龄查询失败,因为无法对数字列执行LIKE。您应该使用=(等于)

希望有帮助


此外,如果您使用的是用户直接在SQL中提供的数据,请考虑切换到预处理语句。目前,您对SQL注入持开放态度。

您应该听听Huw Jones的话


你不想被一家安全公司审计并且有sql注入问题。参数化的查询是mySql支持的

你应该听胡·琼斯的


你不想被一家安全公司审计并且有sql注入问题。参数化的查询是mySql支持的

啊,我明白了。它现在可以工作了,我现在将通过参数化来修复我的查询。谢谢。啊,我明白了。它现在可以工作了,我现在将通过参数化来修复我的查询。谢谢,我明白了。我现在就把这些都修好。谢谢你的回复,请注意。明白了。我现在就把这些都修好。谢谢你的回复,请注意。