请查看以下代码:

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中获取查询并执行,这使我得到了正确的结果。我没有得到问题。

有帮助吗?

解决方案

您正在通过在访问自身中运行的查询和从外部应用程序运行的查询之间的疑问之间的差异来绊倒。

在访问权限中运行查询时,您需要使用星号作为通配符:LIKE 'RT*'

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

其他提示

try ExecuteScalar()方法

替换:

 OleDbDataReader dr1 = qtc.ExecuteReader();
 while (dr1.Read())
 {
    ttCnt = (int)dr1["Count"];
 }
.

 ttCnt = Convert.ToInt32(qtc.ExecuteScalar());
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top