2013-02-13 4 views
0

I이 저장 프로 시저를 실행하는 다음 코드를는 C# ASP.NET에서 마지막 트랜잭션 ID를 가져올 수 없습니다

[WebMethod] 
    public static string addNewNote(string id, string txt) 
    { 
     Guid parentId = new Guid(id); 
     DbProviderFactory dbf = DbProviderFactories.GetFactory(); 
     using (IDbConnection con = dbf.CreateConnection()) 
     { 
      con.Open(); 
      using (IDbTransaction trn = con.BeginTransaction()) 
      { 

       Guid noteId = Guid.Empty; 
       SqlProcs.spNOTES_WebForms 
         (ref noteId, 
         parentId, 
         "Files", 
         "Client Note", 
         txt, 
         true 
         ); 

       IDbCommand cmd = con.CreateCommand(); 
       cmd.CommandText = "SELECT SCOPE_IDENTITY()"; 
       cmd.Transaction = trn; 

       // Get the last inserted id. 
       string insertID = cmd.ExecuteScalar().ToString(); 
       Debug.Print(insertID.ToString()); 
      } 
     } 
     return ""; 
    } 

그것은 출력 패널에 아무것도 출력하지 않습니다. 마지막 ID를 검색하려면 어떻게해야합니까?

참고 : 내 ID는 GUID (고유 식별자)입니다.

편집 :

이 내 저장 프로 시저된다

if exists (select * from dbo.sysobjects where id = object_id(N'spNOTES_WebForms') and OBJECTPROPERTY(id, N'IsProcedure') = 1) 
    Drop Procedure dbo.spNOTES_WebForms; 
GO 

Create Procedure dbo.spNOTES_WebForms 
    (@ID     uniqueidentifier output 
    , @PARENT_ID   uniqueidentifier 
    , @PARENT_TYPE   nvarchar(255) 
    , @MODIFIED_USER_ID  uniqueidentifier 
    , @NAME     nvarchar(255) 
    , @DESCRIPTION   ntext 
    , @VISIBLE_TO_CLIENT bit 
    ) 
with encryption 
as 
    begin 
    set nocount on 

    if dbo.fnIsEmptyGuid(@ID) = 1 begin -- then 
     set @ID = newid(); 
    end -- if; 
    insert into NOTES 
     (ID    
     , PARENT_ID 
     , PARENT_TYPE 
     , CREATED_BY  
     , DATE_ENTERED  
     , MODIFIED_USER_ID 
     , DATE_MODIFIED  
     , NAME    
     , DESCRIPTION  
     , VISIBLE_TO_CLIENT 
     ) 
    values 
     (@ID    
     , @PARENT_ID 
     , @PARENT_TYPE 
     , @MODIFIED_USER_ID 
     , getdate()   
     , @MODIFIED_USER_ID 
     , getdate()   
     , @NAME    
     , @DESCRIPTION  
     , @VISIBLE_TO_CLIENT 
     ); 

    -- 03/04/2006 Paul. Add record to custom table. 
    if not exists(select * from NOTES_CSTM where ID_C = @ID) begin -- then 
     insert into NOTES_CSTM (ID_C) values (@ID); 
    end -- if; 

    end 
GO 

Grant Execute on dbo.spNOTES_WebForms to public; 
GO 
+0

"ID"(기본 키?)가 GUID이면 'SCOPE_IDENTITY()'를 (를) 사용하는 이유는 무엇입니까? 혼란을 피하기 위해'INSERT' 코드와 관련 테이블 구조 (CREATE TABLE' 스크립트)를 보는 것이 도움이 될 것입니다. 저장 프로 시저에서 새 ID를 출력 매개 변수로 반환하는 것을 고려 했습니까? – Pondlife

+0

나는 그것을 고려했다. 그러나 나는 그것을 어떻게하는지 모른다. .. 나는 위의 코드에 내 저장 프로 시저를 추가했다. – user1477388

+1

[이 질문에] (http://stackoverflow.com/questions/339910/retrieve-the-uniqueidentifier-key-value-for-a-record) GUID 값을 얻는 방법을 설명합니다; [이 하나] (http://stackoverflow.com/questions/3433694/how-to-run-the-stored-procedure-that-has-output-parameter-from-c)에서 출력 매개 변수를 가져 오는 방법을 설명합니다. C# – Pondlife

답변

0

먼저 당신이 isDBNull를 확인하거나하지 않겠습니까? 그리고 귀하의 쿼리는 어떤 id도 반환하지 않습니다. string id = cmd.ExecuteScalar(); SELECT SCOPE_IDENTITY(); 끝에 쿼리를 추가하고 다시 시도하십시오. Scope_Identity()는 10 진수를 반환합니다. 쿼리 결과에 Convert.ToInt32를 사용하거나 반환 값을 decimal로 변환 한 다음 int로 캐스팅 할 수 있습니다.

+0

나는 내게 무슨 의미가 있는지 모르겠습니다. 나는 내 목록에서 그것을 볼 수 있기 때문에 기록이 들어가는 것을 압니다. 쿼리가 작동하지만 ID를 검색 할 수 없습니다. 내 ID는 정수 또는 십진수가 아니며 'GUID'입니다. 'GUID'를 검색 할 수 있습니까? – user1477388

+1

scope_identifier()를 다음과 같이 uniqueidentifier로 캐스팅 할 수 있습니다. Guid guid = (Guid) cmd.ExecuteScalar(); – Kadir

+0

"지정된 캐스트가 유효하지 않습니다." 'Guid guid = (Guid) cmd.ExecuteScalar();'를 실행하면됩니다. – user1477388

관련 문제