存储过程输出参数返回@Value [英] Stored procedure output parameter returns @Value

查看:145
本文介绍了存储过程输出参数返回@Value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与这件事挣扎在过去一小时,我敢肯定,我失去了一些东西小,我在SQL Server 2008和C#code的存储过程,我想返回的输出参数我的存储过程。

I'm struggling with this thing for the past hour and I'm sure I'm missing something small, I have a stored procedure in SQL Server 2008 and C# code that I want to return the output parameters of my stored procedure.

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

什么能可能是错误的我或我的code。请帮助!

What can possibly be wrong with me or my code. Please help!

推荐答案

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

You just need to read out the actual values from your output parameters:

 SScore = outScore.Value;

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

The .ToString() doesn't return the value - it returns the name of the parameter instead...

查看的SqlParameter MSDN文档C $ C> 了解更多详情。

See the MSDN documentation on SqlParameter for more details.

这篇关于存储过程输出参数返回@Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆