简体   繁体   中英

SELECT statement with COUNT Always Returns 0

Don't know why I can't reply to people on here with only a small amount of comment text, but my revised code exceeds that so I'm posting new.

This web service always returns 0. If I run it in SSMS it returns 3... not sure why, any ideas?

string ConnString = "Removed";
String query = "DECLARE @userSID varchar(255) SELECT COUNT(AD_SID) As ReturnCount FROM AD_Authorization WHERE AD_SID = @userSID ";

using (OleDbConnection conn = new OleDbConnection(ConnString))
{
   using (OleDbCommand cmd = new OleDbCommand(query, conn))
   {
      cmd.Parameters.AddWithValue("userSID", SpartaCrypto.SpartaEncryptAES(userSID.ToString(), "s3cret!"));
      conn.Open();
      int returnCount = (Int32)cmd.ExecuteScalar();
      conn.Close();

      if (returnCount > 1) 
      {
         return 1;
      }
      else
      {
         return 0;
      }
   }
}

Your query is not a good OLEDB parameterized query.

Try this instead:

"SELECT COUNT(AD_SID) As ReturnCount FROM AD_Authorization WHERE AD_SID = @userSID";

Also, the parameter name should match:

cmd.Parameters.AddWithValue("@userSID", SpartaCrypto.SpartaEncryptAES(userSID.ToString(), "s3cret!"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM