0

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

return (int)comm.ExecuteScalar();

当我在 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();

    }

提前致谢

4

1 回答 1

5

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

using System;

...
return Convert.ToInt32(comm.ExecuteScalar());
于 2012-07-09T20:31:22.670 回答