随笔 - 272  文章 - 7  评论 - 27  阅读 - 83万

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


posted on   NLazyo  阅读(2801)  评论(2编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示