gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:20:28 26 4
gpt4 key购买 nike

在过去的一个小时里,我一直在为这件事苦苦挣扎,我确定我遗漏了一些小东西,我在 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

我或我的代码可能有什么问题。请帮忙!

最佳答案

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

 SScore = outScore.Value;

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

参见 MSDN documentation on SqlParameter了解更多详情。

关于c# - 存储过程输出参数返回@Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12626386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com