SqlDatabase db
=
new
SqlDatabase(
@"
server=.\sql2008;uid=sa;pwd=123456;database=DBTEST
"
);
using (DbConnection connection = db.CreateConnection())
{
// 打开链接
connection.Open();
// 创建事务
DbTransaction Tran = connection.BeginTransaction();
try
{
db.ExecuteNonQuery(Tran, CommandType.Text, " Insert Into TLog (DEC) Values('123') " );
db.ExecuteNonQuery(Tran, CommandType.Text, " Insert Into TLog (DEC) Values('124') " );
db.ExecuteNonQuery(Tran, CommandType.Text, " Insert Into TLog (DEC) Values(125) " );
// 提交事务
Tran.Commit();
}
catch (Exception Ex)
{
// 出错回滚
Tran.Rollback();
Console.WriteLine(Ex.ToString());
}
finally
{
// 关闭连接
connection.Close();
}
Console.WriteLine( " OK " );
Console.ReadKey();
}
using (DbConnection connection = db.CreateConnection())
{
// 打开链接
connection.Open();
// 创建事务
DbTransaction Tran = connection.BeginTransaction();
try
{
db.ExecuteNonQuery(Tran, CommandType.Text, " Insert Into TLog (DEC) Values('123') " );
db.ExecuteNonQuery(Tran, CommandType.Text, " Insert Into TLog (DEC) Values('124') " );
db.ExecuteNonQuery(Tran, CommandType.Text, " Insert Into TLog (DEC) Values(125) " );
// 提交事务
Tran.Commit();
}
catch (Exception Ex)
{
// 出错回滚
Tran.Rollback();
Console.WriteLine(Ex.ToString());
}
finally
{
// 关闭连接
connection.Close();
}
Console.WriteLine( " OK " );
Console.ReadKey();
}
不要忘记在执行时加入“Tran”,否则不起做用的