[예제]C#에서 트랜잭션 사용 하기
먼저 DB에 접속 합니다.
아래는 SQL Server 용 입니다.
//DB에 접속 합니다.
SqlConnection LocalConn;
string ConStr = "server=(local); uid=sa; pwd=******; database=mydb";
LocalConn = new SqlConnection(ConStr);
SqlTransaction transaction=null;
SqlCommand myCommand = null;
SqlDataReader myReader = null;
try
{
LocalConn.Open();
..............
//트랜잭션을 시작 합니다.
transaction = LocalConn.BeginTransaction();
//DML에 대해 Command 객체를 만들때 트랜잭션을 명시 합니다.
sql = "delete from chartmaster ";
sql += " where pid = " + "'" + pid + "'";
sql += " and receiptilsi = " + "'" + Convert.ToDateTime(receiptilsi).ToString("yyyy-MM-dd HH:mm:ss") + "'";
myCommand = new SqlCommand(sql, LocalConn, transaction);
myCommand.ExecuteNonQuery();
...................
sql = "delete from ordermaster ";
sql += " where pid = " + "'" + pid + "'";
ql += " and receiptilsi = " + "'" + Convert.ToDateTime(receiptilsi).ToString("yyyy-MM-dd HH:mm:ss") + "'";
myCommand = new SqlCommand(sql, LocalConn, transaction);
myCommand.ExecuteNonQuery();
.....................
transaction.Commit();
}
catch(Exception ex)
{
transaction.Rollback();
MessageBox.Show(ex.Message, "btnSave_Click");
}
finally
{
LocalConn.Close();
}
댓글 없음:
댓글 쓰기