通过C#的Access数据库查询功能总是返回0 COUNT(*) [英] LIKE query on an Access database via C# always returns COUNT(*) of 0

查看:2247
本文介绍了通过C#的Access数据库查询功能总是返回0 COUNT(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看下面的代码:

using (OleDbConnection openCon = new OleDbConnection(ConfigurationManager.AppSettings["AccessConnectioString"]))
{
                openCon.Open();
                string tc = string.Empty;
                string ttc = string.Empty;
                if (!string.IsNullOrEmpty(QSetId))
                {
                    tc = "select count(*) as [Count] from ABC where QSetId = @qSetId and TText like 'RT*'";
                }
                else
                {
                    tc = "select count(*) as [Count] from PQR where TText like 'RT*'";
                }
                using (OleDbCommand qtc= new OleDbCommand(tc))
                {
                    qtc.Connection = openCon;
                    if (!string.IsNullOrEmpty(QSetId))
                        qtc.Parameters.Add("@qSetId", OleDbType.VarChar).Value = QSetId;
                    OleDbDataReader dr1 = qtc.ExecuteReader();
                    while (dr1.Read())
                    {
                        ttCnt = (int)dr1["Count"];
                    }
                }

                openCon.Close();
}



我总是让算作0,而不是一些整数值。在调试我走查询在MS Access 2013执行,它给了我正确的结果。我没有得到什么问题。

I am always getting count as 0 instead of some integer value. While debugging I take the query and execute in MS ACCESS 2013, it gives me correct result. I am not getting what is the issue.

推荐答案

你正在运行在查询之间绊倒在LIKE通配符的区别。访问本身和外部应用程序运行的查询

You are getting tripped up by the difference in LIKE wildcard characters between queries run in Access itself and queries run from an external application.

在运行查询从Access本身,您需要使用星号作为通配符: LIKE'RT *'

When running a query from within Access itself you need to use the asterisk as the wildcard character: LIKE 'RT*'.

在运行从外部应用程序的查询(如您的C#应用​​程序),您需要使用百分号作为通配符: LIKE'%RT

When running a query from an external application (like your C# app) you need to use the percent sign as the wildcard character: LIKE 'RT%'.

这篇关于通过C#的Access数据库查询功能总是返回0 COUNT(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆