[C#,ADO.NET,오라클]트랜잭션예제,C#,Transaction
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
OleDbDataAdapter adapter = null;
DataSet ds = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ds = new DataSet("emp");
//아래 onj는 $ORACLE_HOME/network/admin에 있는 tnsnames.ora 파읷에 정의된 이름!
string conStr = "Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
using (OleDbConnection connection = new OleDbConnection(conStr))
{
OleDbCommand command = new OleDbCommand();
OleDbTransaction tr = null;
try
{
connection.Open();
tr = connection.BeginTransaction();
command.Connection = connection;
command.Transaction = tr;
command.CommandText = "insert into emp (empno, ename)"
+ " values (5555, '3000길동')";
int i = command.ExecuteNonQuery();
Console.WriteLine(i + "건 Inserted!");
command.CommandText = "insert into emp (empno, ename)"
+ " values (6777, '3000길동')";
i = command.ExecuteNonQuery();
tr.Commit();
adapter = new OleDbDataAdapter("select * from emp", connection);
adapter.Fill(ds, "EMP");
dataGridView1.DataSource = ds.Tables["EMP"];
adapter.Fill(ds, "EMP");
}
catch (Exception ex)
{
tr.Rollback();
MessageBox.Show(ex.Message, "emp Table Loading Error");
}
finally
{
connection.Close();
}
}
}
}
}
댓글 없음:
댓글 쓰기