Why is my SqlCommand returning a string when it should be an int?

前端 未结 9 1330
暗喜
暗喜 2020-12-29 17:06

I have a query that should always be returning a single int. I have now logged it returning a string entirely unrelated to what it should be.

We\'ve been ge

相关标签:
9条回答
  • 2020-12-29 17:29

    There could be more than one WebSite table. Can you qualify the table with schema name:

    SELECT isnull(MAX(PkID),0) FROM YourSchema.WebSite WHERE StoreID = @StoreID AND WebSiteStatusID = @WebSiteStatusID

    0 讨论(0)
  • 2020-12-29 17:32

    The ExecuteScalar() function's return type is object, and you declare your result variable with the var keyword. That's not really a good combination, because you're putting a lot of pressure on the system to get the type inference right.

    0 讨论(0)
  • 2020-12-29 17:33

    Is there any commonality to when it fails to return an int?

    Since your query only ever returns a single column in a single row, what do you get if you use a more typesafe ExecuteReader and take the first column's value?

    Is it always returning a row? If the WHERE clause causes it to return no rows (say your parameters are not what you think they are), your ISNULL doesn't come into effect - there are no rows at all, and ExecuteScalar is supposed to return a NULL.

    0 讨论(0)
提交回复
热议问题