ADO.NET:指定的强制转换无效

use*_*468 0 sql asp.net-mvc stored-procedures casting sql-server-2008

当我执行这样的存储过程时,我得到一个指定的强制转换无效错误:

return (int)comm.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)

当我在SQL Server中执行它时,它返回1,所以我知道我的proc正在运行.

我的演员出了什么问题?

更新:

    public static int IsPresent(string userName, int inTime)
    {
        SqlConnection connObj = new SqlConnection();
        connObj.ConnectionString = Util.SQLConct();
        connObj.Open();

        SqlCommand comm = new SqlCommand("usp_IsUserLocked", connObj);

        comm.CommandType = CommandType.StoredProcedure;

        comm.Parameters.Add(new SqlParameter("@Username", userName));
        comm.Parameters.Add(new SqlParameter("@InTime", inTime));

        return (int)comm.ExecuteScalar();

    }
Run Code Online (Sandbox Code Playgroud)

提前致谢

ale*_*exm 5

您的方法可能返回double或int64或任何其他无法隐式转换为(int)的类型.使用Convert.ToInt32以确保您的方法返回正确的类型.

using System;

...
return Convert.ToInt32(comm.ExecuteScalar());
Run Code Online (Sandbox Code Playgroud)