c#程序中使用"like“查询access数据库语句的问题

   在写使用access数据库的c#程序过程中,遇到各种莫名奇妙的问题。例如使用"like"进行模糊查询,在access查询视图中要使用"*"做模糊匹配(sql中是"%").

   原以为在程序中的查询字符串也应该使用"*",事实上并非如此!

   在access数据库中调试用"*",程序中要改过来用"%",否则是查询不到任何数据的,而且vs还不报任何错,调试都找不到原因。

 

 try
            {
                  _strSql="";
                if (dataGridView1.Rows.Count <= 1)
                {

                    if (dataGridView1.Rows[0].Cells[0].Value == null || dataGridView1.Rows[0].Cells[1].Value == null || dataGridView1.Rows[0].Cells[2].Value == null)
                    {
                        return;
                    }
                    else
                    {
                        string filed =  GetArrayElement(Cxzd, dataGridView1.Rows[0].Cells[0].Value.ToString().Trim(), 1);

                        //string type = GetArrayElement(Cxzd, filed, 2);
                        string cs = Cxfs[dataGridView1.Rows[0].Cells[1].Value.ToString().Trim()].ToString(); 
                        string content = dataGridView1.Rows[0].Cells[2].Value.ToString();
                        if (cs == "like")
                        {
                            content = "'%" + content + "%'";
                        }

                        _strSql = string.Format("where {0} {1} {2}  ", filed, cs, content);
                    }
                }
                   

   后来查资料,原来是连接access驱动程序的问题,由于我的程序中连Access用的是oledb驱动程序,所以在这里 不能用“*”,必须用“%”。如果用的是DAO访问Access数据库,则必须用“*”。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值