2012-09-27 3 views
8

저는 지난 한 시간 동안이 문제에 어려움을 겪고 있습니다. 작지만 뭔가 빠뜨린 것이 틀림 없습니다. 반환 할 SQL Server 2008 및 C# 코드에 저장 프로 시저가 있습니다. 내 저장 프로 시저의 출력 매개 변수.저장 프로 시저 출력 매개 변수에서 @Value를 반환합니다.

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 

. 도와주세요!

답변

12

당신은 당신의 출력 매개 변수에서 실제 을 읽을 필요가 :

SScore = outScore.Value; 

.ToString()을 값을 반환하지 않습니다 - ... 대신 매개 변수의 이름을 반환

자세한 내용은 MSDN documentation on SqlParameter을 참조하십시오.

+0

답장을 보내 주셔서 감사합니다. 분명히 오류를 가져 와서 개체를 변환 할 수 없습니다. 문자열로 변환했지만, 매번 데이터를 삽입하더라도 아무 것도 표시되지 않습니다. 출력 매개 변수를 얻는 방법이라고 생각하십니까? 이 작업을 수행하는 더 좋은 방법이 있습니까? –

0

>이 그 작업을 시도 문의 출력 매개 변수를 얻기 전에 당신은

reader.Close(); 

로 데이터 판독기를 종료해야하고 더 많은 도움을

SScore = outScore.Value.Tostring(); 

로 출력 매개 변수를 얻을 여러 출력 매개 변수에 대해 잘 :

 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();       
      } 
     } 
관련 문제