2012-07-06 43 views
4

我有一個存儲過程返回一個學生是否被鎖定或不:返回從存儲過程的值的方法

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)); 

    } 

回答

15

要在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; 

} 

然而,這是有點凌亂(IMO)。通常我在這種情況下所做的是SELECT我想要的值作爲存儲過程中的最後一條語句。然後我在命令對象上使用ExecuteScalar來檢索值,而不是ExecuteNonQuery

PROC:

... SQL ... 

SELECT @isLocked 

方法:

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(); 
    } 
} 
+0

我可以將這個「ISStudentLocked」方法稱爲布爾方法嗎?像這樣: public static bool IsLockedOut(string studentName) { return(IsUserLocked(studentName,Util.LockoutDays)== 1); } – user793468 2012-07-06 22:18:19

+0

當然,不明白爲什麼不。 – rossipedia 2012-07-06 22:38:25

+0

我得到一個「非靜態字段,方法或屬性'Student.Models.Authorization.IsStudentLocked(字符串,int)需要對象引用」錯誤 – user793468 2012-07-09 14:51:43

3

你應該調用ExecuteScalar而不是ExecuteNonQuery,並用存儲過程中的SELECT替換RETURN。

順便說一句,你應該包裝與using塊的連接,所以即使在Close()之前發生某種異常時也會妥善處置它。

+0

然後我怎麼雖然訪問返回值? – user793468 2012-07-06 21:54:47

+2

它將從這個方法返回... ^^' – Pein 2012-07-06 21:56:20

+0

這個問題特別是關於存儲過程返回一個值。 – Kunal 2017-08-18 21:25:26

0

如果你的存儲過程@IsLocked輸出參數

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

int newValue = (int)paramterIsLockedOut.Value;