我想在一个button上对其Click事件执行对数据库某张表的查询输出运用了下面的代码 DataSet dataset = new DataSet();
MySqlDataAdapter myadapter = new MySqlDataAdapter(comsql,conn);
myadapter.Fill(dataset);
dataGridView1.DataSource = dataset.Tables[0];-------------------这段代码均在button 的click里面第一次查询能成功 但是第二次查询就报错了You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '男';'' at line 1
下面粘贴全部代码
MySqlDataAdapter myadapter = new MySqlDataAdapter(comsql,conn);
myadapter.Fill(dataset);
dataGridView1.DataSource = dataset.Tables[0];-------------------这段代码均在button 的click里面第一次查询能成功 但是第二次查询就报错了You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '男';'' at line 1
下面粘贴全部代码
private void button5_Click(object sender, EventArgs e)
{
string comsql="select * from workerinfo where ";
int count=0;
MySqlConnection conn = new MySqlConnection("User Id=root;server=localhost;Password=like1206;database=hr;Persist Security Info=True");
conn.Open();
if (textBox8.Text != "")
{
comsql = comsql + "name='" + textBox8.Text+"'";
count=1;
}
if (comboBox2.Text != "")
{
if(count==1)
{
comsql=comsql+" and ";
}
comsql = comsql + "sex='" + comboBox2.Text+"'";
count=1;
}
if (textBox9.Text != "")
{
if (count == 1)
{
comsql = comsql + " and ";
}
comsql = comsql + "workernum='" + textBox9.Text+"'";
count = 1;
}
if (textBox7.Text != "")
{
if (count == 1)
{
comsql = comsql + " and ";
}
comsql = comsql + "age='" + textBox7.Text+"'";
count = 1;
}
if (textBox10.Text != "")
{
if (count == 1)
{
comsql = comsql + " and ";
}
comsql = comsql + "jobnum='" + textBox10.Text+"'";
count = 1;
}
if (comboBox3.Text != "")
{
if (count == 1)
{
comsql = comsql + " and ";
}
comsql = comsql + "occupated='" + comboBox3.Text+"'";
count = 1;
}
if (textBox11.Text != "")
{
if (count == 1)
{
comsql = comsql + " and ";
}
comsql = comsql + "id='" + textBox11.Text+"'";
count = 1;
}
if (textBox12.Text != "")
{
if (count == 1)
{
comsql = comsql + " and ";
}
comsql = comsql + "tocontact='" + textBox12.Text+"'";
count = 1;
}
comsql = comsql + ";"; textBox12.Text = comsql;
DataSet dataset = new DataSet();
MySqlDataAdapter myadapter = new MySqlDataAdapter(comsql,conn);
myadapter.Fill(dataset);
dataGridView1.DataSource = dataset.Tables[0];
myadapter.Dispose();
dataset.Dispose();
conn.Close();
comsql="select * from workerinfo where ";
count = 0;
}各位多多帮助下咯~~谢谢
第一次生成了查询条件,但是第二次再点查询按钮的时候.第一次生成的查询条件还保留着,导致拼写sql语句时出现语法错误.建议把进行调试查看要执行的myadapter.SelectCommand.CommandText是否符合sql语法.
最前面加comsql="";试试
1)comsql = comsql + "sex='" + comboBox2.Text+"'";(comboBox2下拉框)
2)数据库中sex的类型是什么?
第一次生成了查询条件,但是第二次再点查询按钮的时候.第一次生成的查询条件还保留着,导致拼写sql语句时出现语法错误.建议把进行调试查看要执行的myadapter.SelectCommand.CommandText是否符合sql语法
用数据库语句 select