/// <summary> ///multi-database server transaction Submission/// </summary> /// <param name= "Sqlstrings" >key is connname,value as SQL statement</param> /// <returns></returns> Public BOOLExecutemultitran (list<string[]>sqlstrings) { BOOLReval =true; SqlCommand cmd=NewSqlCommand (); SqlTransaction Tran; SqlConnection Conn; //transaction Object name, collection of transaction objectsdictionary<string, sqltransaction> Tranresult =Newdictionary<string, sqltransaction>(); //Conn Object name, Objectdictionary<string, sqlconnection> Connresult =Newdictionary<string, sqlconnection>(); //whether the current execution succeeds BOOLIssuccess =true; //list<string> keys =Newlist<string>(); //Loop through Connname for transaction execution foreach(string[] Sqlsinchsqlstrings) { stringKeyName = sqls[0]; //If the current keyname is already present in keys, it means that the Conn has been executed and jumps to the next KeyName execution if(!keys. Contains (KeyName)) {keys. ADD (KeyName); //commits the current conn transaction and fails to mark the current transaction if it fails Try{conn=createconnection (keyName); Conn. Open (); Cmd. Connection=Conn; Tran=Conn. BeginTransaction (); Cmd. Transaction=Tran; //Log Current transactionTranresult.add (KeyName, Tran); //Record Current ConnConnresult.add (KEYNAME, conn); //reads the current conn SQL, executes foreach(string[] SQLinchsqlstrings) { if(sql[0] ==keyName) {Cmd.commandtext= sql[1]; Cmd. ExecuteNonQuery (); } } } Catch(Exception ex) {issuccess=false; } if(!issuccess) { Break; } } } //if the current transaction fails, all the transaction objects that have been executed are rollback if(!issuccess) { foreach(SqlTransaction Sqltraninchtranresult.values) {sqltran.rollback (); } Reval=false; } Else { foreach(SqlTransaction Sqltraninchtranresult.values) {sqltran.commit (); } } //Close Conn foreach(SqlConnection valueinchconnresult.values) {if(value.) State! =connectionstate.closed) {value. Close (); } } returnReval; } PublicSqlConnection CreateConnection (stringkeyName) {SqlConnection sqlconn=NewSqlConnection (Configurationmanager.appsettings[keyname]. ToString ()); returnsqlconn; }
Call:
//1. Deduction of the player's currency//2. Increase the number of gold beans in the player's vault//3. Insert a currency redemption record Private BOOLProcdata (intUseridintPats,intConvertrate,stringClientIP) {List<string[]> Sqls =Newlist<string[]>(); //1. Deduction of the player's currency stringsql ="Update Accountsinfo Set userpat=userpat-"+ pats +"where userid="+UserID; Sqls. ADD (New string[] {"dbaccounts", SQL}); //2. Increase the number of gold beans in the player's vaultsql ="Update Gamescoreinfo Set insurescore=insurescore+"+ pats * convertrate +"where userid="+UserID; Sqls. ADD (New string[] {"dbtreasure", SQL}); //3. Insert a currency redemption recordsql ="INSERT INTO Recordconvertuserpat (recordid,userid,curinsurescore,curuserpat,convertuserpat,convertrate, Isgameplaza,clientip,collectdate) VALUES ("; SQL+= ((int) Gdata.selectvalue ("select Max (RecordID) from Recordconvertuserpat","Dbrecord",0) +1) +","; SQL+ = UserID +","; SQL+ = Gdata.selectvalue ("Select Insurescore from Gamescoreinfo where userid="+ UserID,"dbtreasure",0) +","; SQL+ = Gdata.selectvalue ("Select Userpat from Accountsinfo where userid="+ UserID,"dbaccounts",0) +","; SQL+ = pats +","; SQL+ = Convertrate +","; SQL+="1,"; SQL+="'"+ ClientIP +"',"; SQL+="'"+ System.DateTime.Now.ToString () +"'"; SQL+=")"; Sqls. ADD (New string[] {"Dbrecord", SQL}); returnGdata.executemultitran (SQLS); }