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数据库,则必须用“*”。


原文地址:https://www.cnblogs.com/bile/p/3338748.html