我有一個存儲過程:存儲過程返回空值
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()
將返回空字符串......我想不通在哪裏,我缺少什麼。
空字符串是不一樣的空值。但也許這將有助於http://stackoverflow.com/questions/3309213/getting-return-value-from-stored-procedure-in-ado-net – 2014-11-02 19:02:22
是'USR_CODE'身份列? – Rahul 2014-11-02 19:02:49
是它是標識列 – user2601893 2014-11-02 19:10:12