请查看以下代码:
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 中执行查询并执行,它给出了正确的结果。我不明白问题是什么。
最佳答案
Access 本身运行的查询与外部应用程序运行的查询之间的 LIKE 通配符差异让您感到困惑。
从 Access 本身内部运行查询时,您需要使用星号作为通配符:LIKE 'RT*'
。
从外部应用程序(例如 C# 应用程序)运行查询时,您需要使用百分号作为通配符:LIKE 'RT%'
。
关于c# - 通过 C# 对 Access 数据库进行 LIKE 查询始终返回 COUNT(*) 为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016044/