2014-09-26 2 views
1

불행히도 업데이트 쿼리 후에받은 값을 밀어 넣을 수있는 문제를 찾을 수 없습니다. 나는이 SQL 명령이 있습니다C#에서 UPDATE .. OUTPUT 값을 읽는 방법?

@"if exists (select Id from Documents where Id=3) 
       begin 
        update Documents set Modified='Jackson',ModifiedDate=(getdate()),Start= getdate(),Finish=(select dateadd(year,Termen,getdate()) from DocumentTypes where [Key]='ITO') 
        output deleted.FilePath as OldFile, inserted.FilePath as NewFile 
        where Id=3 
       end" 

을 내가

string old = cmd.Parameters["OldFile"].value; 

로 접근을 시도하지만 그것은 작동하지 않습니다 내가 다른 솔루션이 필요합니다.

경우

SqlParameter.Direction property 
+0

질문을 이해하기 쉽게 표시하고 더 설명해야합니다. –

+0

[SqlParameter.Direction] (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.direction (v = vs.110) .aspx) 속성을 찾으십니까? – Reniuz

+1

(컴파일러 오류 *가 문제가 아니라면 컴파일시에 코드를 게시해야합니다.) 게시물에서'.values'는 매우 틀리며, 아마도 무의미한 탄젠트를 피하기 위해 필요한 경우 게시물을 수정하십시오.) – user2864740

답변

0
당신은 'SqlParameter.Direction'속성을 사용하지 않고 그것을 만들 수있는 경우 요청

withoute 그것을 만들 수 있습니다, 나는 대답은 아니, 그렇지 "라고 생각합니다 ". 어쨌든, 이것은 당신이 당신이 그 속성을 사용하여 질문 한 것을 할 수있는 방법입니다. 당신 ADO.net 프레임 워크 솔루션에서 다음

CREATE PROCEDURE GetFilePaths 
@OldFile nvarchar(1000) output, 
@NewFile nvarchar(1000) output 
AS 

if exists (select Id from Documents where Id=3) 
      begin 
       update Documents set Modified='Jackson',ModifiedDate=(getdate()),Start= getdate(),Finish=(select dateadd(year,Termen,getdate()) from DocumentTypes where [Key]='ITO') 
       where Id=3 
       set @OldFile = deleted.FilePath 
       set @NewFile = inserted.FilePath 
      end 

, 당신이 뭔가를해야한다 : 먼저 수행하여 SQL 관리 Studio에서 SQL 명령 (인라인 SQL은 안전하지 않은 코드에 명령)의 저장 프로 시저를해야한다 like :

 public static void GetPaths() 
    { 
     //substitute DB.GetSqlConnection() for your way of getting your connection 
     using (SqlConnection conn = DB.GetSqlConnection()) 
     { 

      using (SqlCommand cmd = conn.CreateCommand()) 
      { 
       cmd.CommandText = @"GetFilePaths" 
       cmd.CommandType = System.Data.CommandType.StoredProcedure; 

       //Declare the output parameters you're gonna use in your stored procedure 
       SqlParameter oldFile_param = new SqlParameter("OldFile", System.Data.SqlDbType.NVarChar, 1000); 
       oldFile_param.Direction = System.Data.ParameterDirection.Output; 
       SqlParameter newFile_param= new SqlParameter("NewFile", System.Data.SqlDbType.NVarChar, 1000); 
       newFile_param.Direction = System.Data.ParameterDirection.Output; 

       cmd.Parameters.Add(oldFile_param); 
       cmd.Parameters.Add(newFile_param); 

       cmd.ExecuteNonQuery(); 

       object oldFile = oldFile_param.Value; 
       object newFile = newFile_param.Value; 

      } 
     } 
    } 

원하는 파일 경로는 'object oldFile'및 'object newFile'입니다.

관련 문제