using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace _03演示ado.net中的事务
{
class Program
{
static void Main(string[] args)
{
#region MyRegion
string constr = "Data Source=zxtiger;Initial Catalog=itcastcn;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
//通过Connection创建一个事务对象
SqlTransaction tran = con.BeginTransaction();
string sql = "delete from TblStudent where tsid=4";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Transaction = tran;
int r = cmd.ExecuteNonQuery();
Console.WriteLine("所影响的行数{0}", r);
//tran.Rollback();
//Console.WriteLine("回滚了。");
tran.Commit();
Console.WriteLine("提交了。。");
}
}
Console.WriteLine("ok");
Console.ReadKey();
#endregion
}
}
}