《个人经验,仅供参考》
//创建数据库连接
OleDbConnection connection = new OleDbConnection(ConnectString.getConnectString());
//注意,这里是一个陷阱,不写会出恶心的异常,因此,使用之前最好判断一下为好。
if (connection.State != ConnectionState.Open)
connection.Open();
//开始事物,执行实务操作:
OleDbTransaction myTrans = connection.BeginTransaction();
//绑定事物语句::
//sql_1
string sql_1 = "update User_Cards set isUsed=1 where cardId="+cardId;
OleDbCommand command_1 = new OleDbCommand(sql_1,connection, myTrans);
//sql_2
string sql_2 = "update userBanlance set score = score + "+faceValue +"where userId = "+userId;
OleDbCommand command_2 = new OleDbCommand(sql_2,connection, myTrans);
//sql_3
string sql_3 = String.Format("insert into chargeLog(userId,cardId,chargeTypeId,chargeTime) values('{0}','{1}','{2}','{3}')",userId,cardId,0,"时间");
OleDbCommand command_3 = new OleDbCommand(sql_3, connection, myTrans);
//执行事物语句:
try { command_1.ExecuteNonQuery(); command_2.ExecuteNonQuery(); command_3.ExecuteNonQuery();
//提交
myTrans.Commit(); successState=true; }catch{ myTrans.Rollback(); } finally { command_1.Dispose(); command_2.Dispose(); command_3.Dispose();
//注意这里的写法,这是一个陷阱,如果不写,会出现异常(在下一遍博文里叙述)
if (connection.State != ConnectionState.Closed) { connection.Close(); } }
……………………