存储过程输出参数返回@Value

时间:2012-09-27 16:53:44

标签: c# asp.net sql-server-2008

我在过去一小时内一直在努力解决这个问题,我确信我遗漏了一些小东西,我在SQL Server 2008和C#代码中有一个存储过程,我希望返回存储过程的输出参数。

SQL:

Alter Procedure dbo.GetAssessment
    @UserID int,
    @AssessmentName varchar(255),
    @Score varchar(100) output,
    @Completed varchar(10) output,
    @DisplayName nvarchar(128) output,
    @Result varchar(2500) output
as
begin
        select @Score = A.Score, @Completed = A.Completed, @DisplayName = U.Displayname, @Result = A.Result 
        from Assessment A 
            inner join Users U
            on U.UserId = A.UserID
        where U.UserID = @UserId
        and AssessmentName = @AssessmentName

end
GO

C#

String SScore, SName, SResult, SComp;
            lblAsse.Text = Request.QueryString["AID"];

            InsertAssessment(lblAsse.Text, "No", 2, "N/A", "N/A");

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString))
            {
                SqlParameter outScore = new SqlParameter("@Score", SqlDbType.VarChar,100){ Direction = ParameterDirection.Output };
                SqlParameter outComp = new SqlParameter("@Completed", SqlDbType.VarChar,10){ Direction = ParameterDirection.Output };
                SqlParameter outName = new SqlParameter("@DisplayName", SqlDbType.NVarChar, 128) { Direction = ParameterDirection.Output };
                SqlParameter outResult = new SqlParameter("@Result", SqlDbType.VarChar,2500){ Direction = ParameterDirection.Output };              

                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "GetAssessment";
                cmd.Parameters.AddWithValue("@AssessmentName", lblAsse.Text);
                cmd.Parameters.AddWithValue("@UserId", 2);
                cmd.Parameters.Add(outScore);
                cmd.Parameters.Add(outComp);
                cmd.Parameters.Add(outName);
                cmd.Parameters.Add(outResult);
                cmd.ExecuteScalar();

                SScore = outScore.ToString();
                SName = outName.ToString();
                SResult = outResult.ToString();
                SComp = outComp.ToString();

                conn.Close();

                lblAsse.Text = SScore;`

输出:

@Score

我或我的代码可能出错。请帮忙!

3 个答案:

答案 0 :(得分:13)

您只需要从输出参数中读出实际的

 SScore = outScore.Value;

.ToString()不返回值 - 而是返回参数的名称......

有关详细信息,请参阅MSDN documentation on SqlParameter

答案 1 :(得分:0)

只需要这样做。 在获取输出参数之前,必须将数据读取器关闭为

    reader.Close();

然后输出

输出参数
    SScore = outScore.Value.Tostring();

如需更多帮助,请参阅此http://msdn.microsoft.com/en-us/library/ms971497

答案 2 :(得分:0)

>尝试将其工作正常用于多输出参数:

     using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conStringEndicia"].ConnectionString)){

            using (var sqlCmd = new SqlCommand("endicia.credentialLookup", sqlConnection))
            { 

                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@accountNumber", accountNumber);
                SqlParameter outLogin = new SqlParameter("@login", SqlDbType.NVarChar, 100) { Direction = ParameterDirection.Output };
                sqlCmd.Parameters.Add(outLogin);
                SqlParameter outPassword = new SqlParameter("@password", SqlDbType.NVarChar, 100) { Direction = ParameterDirection.Output };
                sqlCmd.Parameters.Add(outPassword);
                sqlConnection.Open();
                sqlCmd.ExecuteNonQuery();
                string login, password;
                login = outLogin.Value.ToString();
                password = outPassword.Value.ToString();                        
            }
        }