读书人

C#调用Aceess数据库,该如何处理

发布时间: 2013-12-06 17:56:43 作者: rapoo

C#调用Aceess数据库
C#调用Aceess数据库进行查询时可以查询,但当选中复选框时总是报错“语法错误(操作符丢失)在查询表达式'p_sex性别from Persons where p_name like %宝宝%中;界面及代码如下,各位大神帮忙看看,弄了好久都没弄出来!C#调用Aceess数据库,该如何处理
其中一段代码:
private void button1_Click(object sender, EventArgs e)
{

if (textBox1.Text.Trim() != "")
{
Bind1();
label6.Text = "您按 查号 查询的结果如下:";
}
if (textBox2.Text.Trim() != "")
{
Bind2();
label6.Text = "您按 姓名 查询的结果如下:";
}
if (textBox3.Text.Trim() != "")
{
Bind3();
label6.Text = "您按 年龄 查询的结果如下:";
}
if (textBox4.Text.Trim() != "")
{
Bind4();
label6.Text = "您按 籍贯 查询的结果如下:";
}
}
private void Bind2()
{
if (textBox2.Text.Trim() != "")
{
string strPath = Application.StartupPath + "\\CHAN.mdb";
string ConStr = " Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + strPath;
using (OleDbConnection Olecon = new OleDbConnection(ConStr))
try
{
Olecon.Open();
String qbll = null;
qbll = qbll + "select p_id ,p_name ,p_age , p_jig ";
if (checkBox1.Checked == true)
{

qbll = qbll + ",p_sex " + checkBox1.Text.Trim();
}
if (checkBox2.Checked == true)
{
qbll = qbll + ",p_shengri " + checkBox2.Text.Trim();
}
if (checkBox3.Checked == true)


{
qbll = qbll + ",p_sfzh " + checkBox3.Text.Trim();
}
if (checkBox4.Checked == true)
{
qbll = qbll + ",p_hyzk " + checkBox4.Text.Trim();
}
if (checkBox5.Checked == true)
{
qbll = qbll + ",p_xueli " + checkBox5.Text.Trim();
}
if (checkBox6.Checked == true)
{
qbll = qbll + ",p_mobile " + checkBox6.Text.Trim();
}
if (checkBox7.Checked == true)
{
qbll = qbll + ",p_email " + checkBox7.Text.Trim();
}
if (checkBox8.Checked == true)
{
qbll = qbll + ",p_bumen " + checkBox8.Text.Trim();
}
if (checkBox9.Checked == true)
{
qbll = qbll + ",p_zhiwei " + checkBox9.Text.Trim();
}
if (checkBox10.Checked == true)
{
qbll = qbll + ",p_renyleib " + checkBox10.Text.Trim();
}
if (checkBox11.Checked == true)
{


qbll = qbll + ",p_yonggxings " + checkBox11.Text.Trim();
}
if (checkBox12.Checked == true)
{
qbll = qbll + ",p_bianhao " + checkBox12.Text.Trim();
}
if (checkBox13.Checked == true)
{
qbll = qbll + ",p_ruzhisj " + checkBox13.Text.Trim();
}
if (checkBox14.Checked == true)
{
qbll = qbll + ",p_sage " + checkBox14.Text.Trim();
}
if (checkBox15.Checked == true)
{
qbll = qbll + ",p_hetongks " + checkBox15.Text.Trim();
}
if (checkBox16.Checked == true)
{
qbll = qbll + ",p_hetongzz " + checkBox16.Text.Trim();
}
if (checkBox17.Checked == true)
{
qbll = qbll + ",p_beizhu " + checkBox17.Text.Trim();
}
if (checkBox18.Checked == true)
{
qbll = qbll + ",p_gongzdw1 " + checkBox18.Text.Trim();
}
if (checkBox19.Checked == true)
{
qbll = qbll + ",p_xuexyx1 " + checkBox19.Text.Trim();


}
if (checkBox20.Checked == true)
{
qbll = qbll + ",p_peixdw1 " + checkBox20.Text.Trim();
}
if (checkBox22.Checked == true)
{
qbll = qbll + ",p_jlbeizhu " + checkBox22.Text.Trim();
}
qbll = qbll + "from Persons where p_name like '%" + textBox2.Text.Trim() + "%'";

//OleDbConnection oleCon = new OleDbConnection(ConStr);
//OleDbDataAdapter oleDap = new OleDbDataAdapter("select * from Persons", oleCon);
OleDbDataAdapter oleDap = new OleDbDataAdapter(qbll , Olecon);
ds = new DataSet();
oleDap.Fill(ds, "Persons");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
/*oleCon.Close();
oleCon.Dispose();*/
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
finally
{
Olecon.Close();
}
}
}
[解决办法]
qbll = qbll + " from Persons where p_name like '%" + textBox2.Text.Trim() + "%'";


, 你这from前面是不是少空格 ,你这句设个断点,看qb11得到是什么值,就知道哪里错了
[解决办法]
qbll = qbll + " from Persons where p_name like '%" + textBox2.Text.Trim() + "%'";//from前面加个空格
[解决办法]
select p_id ,p_name ,p_age , p_jig ,p_sex " + checkBox1.Text.Trim(),字段是“p_sex 性别”,当中有空格?

[解决办法]
var checkboxs = new CheckBox[] { checkBox1, checkBox2, checkBox3, ... }
var fields = new string[] { "p_sex", "p_shengri", "p_sfzh", ... };
qbll += ", " + string.Join(", ", checkboxs.Zip(fields, (x, y) => x.Checked ? y : "").Where(x => x != ""));

三行代码就能写好的东西你写了快50行。C#调用Aceess数据库,该如何处理

你们老板按照代码行数发工资?

读书人网 >C#

热点推荐