2015년 4월 27일 월요일

C# TransactionScope에 대한 데모 스크립트 입니다.

먼저 회원 테이블 생성 스크립트 입니다.
Create Table Members
(
id int not null,
name varchar(10) not null,
title varchar(10) not null,
address varchar(50) not null,
iDate datetime not null default (getdate()),
constraint PK_Members_id Primary Key (id)
)
go

Insert Into Members(id, name, title, address) Values(1, '홍길동', '대리', '서울시 강남구 역삼동')
Insert Into Members(id, name, title, address) Values(2, '이순신', '대리', '서울시 강남구 역삼동2')
Insert Into Members(id, name, title, address) Values(3, '장길산', '과장', '서울시 강남구 역삼동3')
Insert Into Members(id, name, title, address) Values(4, '문길래', '부장', '서울시 강남구 역삼동4')
Insert Into Members(id, name, title, address) Values(5, '일지매', '사원', '서울시 강남구 역삼동5')

Select * From Members

--저장프로시져 생성
Create  Proc  up_Members_insert
(
@name varchar(10),
@title varchar(10),
@address varchar(50)
)
As
Declare @id int
Select @id = max(id) From Members
Set @id = @id+1
Insert Into Members (id, name, title, address)
Values (@id, @name, @title, @address)
--실행
up_members_insert 'bb', 'bbb', 'bbbb'

-------------------
TransactionScope데모를 위한 스크립트 입니다.
use test1
go

create table t1
(
id int identity primary key,
name varchar(10) not null
)

use test2
go

create table t2
(
id int identity primary key,
name varchar(10) not null
)


C# 데모 코드입니다. 

  try
            {
                // Create the TransactionScope to execute the commands, guaranteeing
                // that both commands can commit or roll back as a single unit of work.
                using (TransactionScope scope = new TransactionScope())
                {
                    using (SqlConnection connection1 = new SqlConnection(
                        @"server=.\sqlExpress;database=test1;integrated security=true"))
                    {
                        // Opening the connection automatically enlists it in the 
                        // TransactionScope as a lightweight transaction.
                        connection1.Open();

                        // Create the SqlCommand object and execute the first command.
                        SqlCommand command1 = new SqlCommand(
                            "Insert Into t1 (name) Values('AA')", connection1);
                        int returnValue = command1.ExecuteNonQuery();
                        Debug.WriteLine(returnValue); 

                        // If you get here, this means that command1 succeeded. By nesting
                        // the using block for connection2 inside that of connection1, you
                        // conserve server and network resources as connection2 is opened
                        // only when there is a chance that the transaction can commit.   
                        using (SqlConnection connection2 = new SqlConnection(
                            @"server=.\sqlExpress;database=test2;integrated security=true"))
                        {
                            // The transaction is escalated to a full distributed
                            // transaction when connection2 is opened.
                            connection2.Open();

                            // Execute the second command in the second database.
                            returnValue = 0;
                            SqlCommand command2 = new SqlCommand(
                                "Insert Into t2 (name) Values('BB')", connection2);
                            returnValue = command2.ExecuteNonQuery();
                            Debug.WriteLine(returnValue); 
                        }
                    }

                    // The Complete method commits the transaction. If an exception has been thrown,
                    // Complete is not  called and the transaction is rolled back.
                    scope.Complete();

                }

            }
            catch (TransactionAbortedException ex)
            {
                MessageBox.Show(ex.Message); 
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message); 
            }

댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.

5월 14일 새벽에 chatGPT 4o가 발표되었습니다. 옵티마이즈, 옴니라는 의미인데 실시간 통역, 다자간 회의, 멀티모달 기능의 강화등이 보이네요.

  초격차로 OpenAI진영이 다시 앞서가는 모양을 보여주고 있습니다. 저도 새벽에 일어나자 마자 올라온 영상들과 글을 정리하고 있습니다. ㅎㅎ 영화 HER의 사진이 새벽에 많이 올라왔었는데 저도 안본 영화입니다. 주말에 한번 봐야 할 것 같습니다....