C# 将值从存储过程返回给方法

C# 将值从存储过程返回给方法,c#,stored-procedures,C#,Stored Procedures,我有一个存储过程,它返回学生是否被锁定: RETURN @isLocked 我执行以下存储过程: public int IsStudentLocked(string studentName, int lockoutTime) { SqlConnection connObj = new SqlConnection(); connObj.ConnectionString = Util.StudentDataInsert(); conn

我有一个存储过程,它返回学生是否被锁定:

RETURN @isLocked
我执行以下存储过程:

    public int IsStudentLocked(string studentName, int lockoutTime)
    {
        SqlConnection connObj = new SqlConnection();
        connObj.ConnectionString = Util.StudentDataInsert();
        connObj.Open();

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

        comm.CommandType = CommandType.StoredProcedure;

        comm.Parameters.Add(new SqlParameter("@Studentname", studentName));
        comm.Parameters.Add(new SqlParameter("@LockoutTime", lockoutTime));

        comm.ExecuteNonQuery();
        connObj.Close();

        //How can I return the @isLocked value below?
        return ((int)(@isLocked));

    }

您应该调用ExecuteScalar而不是ExecuteOnQuery,并在存储过程中将RETURN替换为SELECT


顺便说一句,您应该使用块用
包装您的连接,这样即使在Close()之前发生异常的情况下,它也会被正确地处理。

要使用T-SQL中的
RETURN
语句(只能返回整数值),您必须添加一个参数来检索它:

public int IsStudentLocked(string studentName, int lockoutTime)
{
    SqlConnection connObj = new SqlConnection();
    connObj.ConnectionString = Util.StudentDataInsert();
    connObj.Open();

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

    comm.CommandType = CommandType.StoredProcedure;

    comm.Parameters.Add(new SqlParameter("@Studentname", studentName));
    comm.Parameters.Add(new SqlParameter("@LockoutTime", lockoutTime));

    var returnParam = new SqlParameter
    {
        ParameterName = "@return",
        Direction = ParameterDirection.ReturnValue
    };

    comm.Parameters.Add(returnParam);

    comm.ExecuteNonQuery();

    var isLocked = (int)returnParam.Value;
    connObj.Close();

    return isLocked;

}
然而,这有点混乱(国际海事组织)。通常在这种情况下,我要做的是
选择我想要作为存储过程中最后一条语句的值。然后我在命令对象上使用
ExecuteScalar
来检索值,而不是
ExecuteNonQuery

过程:

方法:

public int IsStudentLocked(string studentName, int lockoutTime)
{
    using(SqlConnection connObj = new SqlConnection())
    {
        connObj.ConnectionString = Util.StudentDataInsert();
        connObj.Open();

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

        comm.CommandType = CommandType.StoredProcedure;

        comm.Parameters.Add(new SqlParameter("@Studentname", studentName));
        comm.Parameters.Add(new SqlParameter("@LockoutTime", lockoutTime));

        return (int)comm.ExecuteScalar();
    }
}

如果@islock在存储过程中锁定了一个输出参数

System.Data.SqlClient.SqlParameter paramterIsLockedOut = command1.Parameters.Add("@MyParameter", SqlDbType.SmallInt);
paramterIsLockedOut.Direction = ParameterDirection.Output;
command1.ExecuteNonQuery();

int newValue = (int)paramterIsLockedOut.Value;

那么我如何访问返回值呢?它将从此方法返回…^^'这个问题是关于存储过程
返回
值的。我可以将这个“ISStudentLocked”方法称为布尔方法吗?类似这样的内容:public static bool IsLockedOut(string studentName){return(IsUserLocked(studentName,Util.LockoutDays)==1)}我得到一个“非静态字段、方法或属性‘Student.Models.Authorization.IsStudentLocked(string,int)’需要对象引用”错误只需将我提供的方法设置为静态。这真的是一个完全不同的问题。
System.Data.SqlClient.SqlParameter paramterIsLockedOut = command1.Parameters.Add("@MyParameter", SqlDbType.SmallInt);
paramterIsLockedOut.Direction = ParameterDirection.Output;
command1.ExecuteNonQuery();

int newValue = (int)paramterIsLockedOut.Value;