Cross-database transactions

Source: Internet
Author: User

/// <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); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.