[英]C# MYSQL - Search filtering a datagridview with a combobox and textbox
嗨,我正在嘗試使用combobox
和textbox
來搜索篩選datagridview
。
我已成功完成此操作,但只有在搜索ID列時才能正常工作。 其他列只是崩潰,顯示以下消息:
您的SQL語法有誤; 檢查與您的MariaDB服務器版本相對應的手冊以獲取正確的語法以在第1行的'Name LIKE'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)
MySqlCommand cmd = new MySqlCommand(@"select * from
clinic_inventory_system.patient WHERE `Last Name` LIKE ....";
話雖如此,請考慮盡快將您的查詢更改為使用參數化查詢
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 Injection攻擊,並且,如果“ First Name
包含單引號,則不會再出現語法錯誤
首先,使用名字,姓氏和聯系電話,您需要正確地對列進行轉義。 由於您使用的是MariaDB,因此應使用反引號(`)對列名進行轉義。
其次,您的Age查詢失敗,因為您無法對數字列執行LIKE。 您應該使用=(等於)。
希望能有所幫助。
另外,如果您使用的是用戶直接在SQL中提供的數據,請考慮切換到預備語句。 目前,您可以使用SQL注入。
您應該聽聽Huw Jones。
您不想受到安全公司的審核,並且遇到sql注入問題。 參數化查詢是mySql支持的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.