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
"ID"(기본 키?)가 GUID이면 'SCOPE_IDENTITY()'를 (를) 사용하는 이유는 무엇입니까? 혼란을 피하기 위해'INSERT' 코드와 관련 테이블 구조 (CREATE TABLE' 스크립트)를 보는 것이 도움이 될 것입니다. 저장 프로 시저에서 새 ID를 출력 매개 변수로 반환하는 것을 고려 했습니까? – Pondlife
나는 그것을 고려했다. 그러나 나는 그것을 어떻게하는지 모른다. .. 나는 위의 코드에 내 저장 프로 시저를 추가했다. – user1477388
[이 질문에] (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