请查看以下代码:
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中执行,它给了我正确的结果。我没有得到什么问题。
答案 0 :(得分:4)
由于在Access本身运行的查询和从外部应用程序运行的查询之间的LIKE通配符的不同,您将被绊倒。
从Access本身运行查询时,您需要使用星号作为通配符:LIKE 'RT*'
。
从外部应用程序(如C#应用程序)运行查询时,您需要使用百分号作为通配符:LIKE 'RT%'
。
答案 1 :(得分:0)
尝试ExecuteScalar()
方法
替换它:
OleDbDataReader dr1 = qtc.ExecuteReader();
while (dr1.Read())
{
ttCnt = (int)dr1["Count"];
}
有了这个:
ttCnt = Convert.ToInt32(qtc.ExecuteScalar());