LIKE query on an Access database via C# always returns COUNT(*) of 0

后端 未结 2 619
粉色の甜心
粉色の甜心 2021-01-15 03:39

Please look into following code:

using (OleDbConnection openCon = new OleDbConnection(ConfigurationManager.AppSettings[\"AccessConnectioString\"]))
{
                


        
相关标签:
2条回答
  • 2021-01-15 04:01

    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.

    When running a query from within Access itself you need to use the asterisk as the wildcard character: 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%'.

    0 讨论(0)
  • 2021-01-15 04:03

    Try ExecuteScalar() method

    Replace This:

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

    With This:

     ttCnt = Convert.ToInt32(qtc.ExecuteScalar());
    
    0 讨论(0)
提交回复
热议问题