2014-11-02 124 views
0

我有一個存儲過程:存儲過程返回空值

ALTER PROCEDURE usp_ins_usr_mst   
    @USR_NAME VARCHAR(max),   
    @USR_MAIL VARCHAR(max),  
    @USR_CODE INT OUTPUT   
AS   
BEGIN   
    SET NOCOUNT ON; 

    IF NOT EXISTS (SELECT * FROM USR_MST WHERE [USR_mail] = @USR_MAIL)   
    BEGIN   
     INSERT INTO USR_MST([USR_NAME], USR_MAIL) 
     VALUES(@USR_NAME, @USR_MAIL);   
    END   

    SET @USR_CODE = (SELECT USR_CODE FROM USR_MST WHERE USR_MAIL = @USR_MAIL) ; 
    select @USR_CODE ; 
END 

和下面的C#代碼:

using (SqlConnection cn = new SqlConnection(cm.sqlCnnString)) 
{ 
    cn.Open(); 
    SqlCommand cmd = new SqlCommand("exec usp_ins_usr_mst @user, @email, @USR_CODE", cn); 
    cmd.Parameters.AddWithValue("@user", " "); 
    cmd.Parameters.AddWithValue("@email", HttpContext.Current.Session["uid"].ToString()); 

    cmd.Parameters.Add("@USR_CODE", SqlDbType.Int); 
    cmd.Parameters["@USR_CODE"].Direction = ParameterDirection.Output; 

    cmd.CommandTimeout = 15000; 
    cmd.ExecuteNonQuery(); 
    cn.Close(); 

    HttpContext.Current.Session["USR_CODE"] = cmd.Parameters["@USR_CODE"].Value.ToString(); 
} 

但這cmd.Parameters["@USR_CODE"].Value.ToString()將返回空字符串......我想不通在哪裏,我缺少什麼。

+0

空字符串是不一樣的空值。但也許這將有助於http://stackoverflow.com/questions/3309213/getting-return-value-from-stored-procedure-in-ado-net – 2014-11-02 19:02:22

+0

是'USR_CODE'身份列? – Rahul 2014-11-02 19:02:49

+0

是它是標識列 – user2601893 2014-11-02 19:10:12

回答

0

嘗試更換此

SqlCommand cmd = new SqlCommand("exec usp_ins_usr_mst @user,@email,@USR_CODE", cn); 

這個(添加OUTPUT子句來執行)

SqlCommand cmd = new SqlCommand("exec usp_ins_usr_mst @user,@email,@USR_CODE OUTPUT", cn); 

create proC#test_proc 
(
    @param_in int, 
    @param_out int OUTPUT 
) as 
begin 
    set @param_out = @param_in; 
end 
go 

declare 
    @out1 int, 
    @out2 int; 

exeC#test_proc 1, @out1;  -- returns NULL 
exeC#test_proc 1, @out2 OUTPUT; -- returns 1 

select @out1; 
select @out2; 

drop proC#test_proc 
+0

謝謝安德烈謝謝@rahul – user2601893 2014-11-02 19:29:53

+0

@Rahul我認爲'方向'只有在'CommandType = CommandType.StoredProcedure'時纔有意義,但在我們的例子中使用了exec表達式,因此它必須滿足存儲過程的執行規則。 .. – 2014-11-02 19:42:14

0

首先改變你的程序來使用SCOPE_IDENTITY()而不是像

ALTER PROCEDURE usp_ins_usr_mst   
@USR_NAME VARCHAR(max),   
@USR_MAIL VARCHAR(max),  
@USR_CODE INT OUTPUT   
AS   
BEGIN   
    SET NOCOUNT ON; 
IF NOT EXISTS (SELECT * FROM USR_MST WHERE [USR_mail] = @USR_MAIL)   
BEGIN   
    INSERT INTO USR_MST([USR_NAME],USR_MAIL) VALUES(@USR_NAME,@USR_MAIL);   
END 

SELECT @USR_CODE = SCOPE_IDENTITY(); 
END 

儘量保持參數名稱與過程相同。你做錯了一個錯誤;您正在嘗試在關閉連接後獲取OUTPUT參數值(如下所示);而你應該在關閉連接之前得到它。

cn.Close(); 

Session["USR_CODE"] = cmd.Parameters["@USR_CODE"].Value.ToString(); 

修改代碼,如下圖所示

 SqlCommand cmd = new SqlCommand("dbo.usp_ins_usr_mst", cn); 

     cmd.CommandType = CommandType.StoredProcedure; 

     cmd.Parameters.AddWithValue("@USR_NAME", " "); 
     cmd.Parameters.AddWithValue("@USR_MAIL", Session["uid"].ToString()); 

     cmd.Parameters.Add("@USR_CODE", SqlDbType.Int); 
     cmd.Parameters["@USR_CODE"].Direction = ParameterDirection.Output; 
     cmd.CommandTimeout = 15000; 
     cmd.ExecuteNonQuery(); 

     Session["USR_CODE"] = cmd.Parameters["@USR_CODE"].Value.ToString(); 

     cn.Close(); 
+0

謝謝@rahul .... – user2601893 2014-11-02 19:30:55