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

前端 未结 9 1388
暗喜
暗喜 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:14

    I think that you were thinking about sqlCommand.ExecuteNonQuery that returns the number of rows affected within an int value...

    This is the definition of the ExecuteScalar method:

    public override object ExecuteScalar()
    Member of System.Data.SqlClient.SqlCommand
    

    Summary:

    Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

    Returns:

    The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty.

    So, I think that the common way of returning that column is as a string representation of the column value.

提交回复
热议问题