Sqldatabase DB = New Sqldatabase ( @" Server =. \ sql2008, uid = SA, Pwd = 123456, database = dbtest " );
Using (Dbconnection connection = DB. createconnection ())
{
// Open Link
Connection. open ();
// Create a transaction
Dbtransaction tran = Connection. begintransaction ();
Try
{
DB. executenonquery (Tran, commandtype. Text, " Insert into tlog (DEC) values ('20140901 ') " );
DB. executenonquery (Tran, commandtype. Text, " Insert into tlog (DEC) values ('20140901 ') " );
DB. executenonquery (Tran, commandtype. Text, " Insert into tlog (DEC) values (125) " );
// Commit transactions
Tran. Commit ();
}
Catch (Exception ex)
{
// Error rollback
Tran. rollback ();
Console. writeline (ex. tostring ());
}
Finally
{
// Close connection
Connection. Close ();
}
Console. writeline ("OK");
Console. readkey ();
}
Do not forget to add "Tran" during execution; otherwise, it cannot be used.