sqlserver跨数据库事物同步(转vinsonLu)

sqlserver跨数据库事物同步(转vinsonLu),第1张

概述/// <summary> /// 多数据库服务器事务提交 /// </summary> /// <param name="sqlStrings">key为connName,value为Sql语句</param> /// <returns></returns> public bool ExecuteMultiTran(List<string[]> sqlSt
/// <summary>    /// 多数据库服务器事务提交    /// </summary>    /// <param name="sqlStrings">key为connname,value为SQL语句</param>    /// <returns></returns>    public bool ExecuteMultiTran(List<string[]> sqlStrings)    {        bool reval = true;        sqlCommand cmd = new sqlCommand();        sqlTransaction tran;        sqlConnection conn;        //事务对象名,事务对象的集合        Dictionary<string,sqlTransaction> tranResult = new Dictionary<string,sqlTransaction>();        //conn对象名,对象        Dictionary<string,sqlConnection> connResult = new Dictionary<string,sqlConnection>();        //当前是否执行成功        bool isSuccess = true;        //        List<string> keys = new List<string>();        //通过connname进行循环执行事务        foreach (string[] sqls in sqlStrings)        {            string keyname = sqls[0];            //如果keys中已经存在当前 keyname,说明改conn的已经执行完毕,跳到下一keyname执行            if (!keys.Contains(keyname))            {                keys.Add(keyname);                //提交当前conn的事务,如果失败,标记当前事务失败                try                {                    conn = CreateConnection(keyname);                    conn.open();                    cmd.Connection = conn;                    tran = conn.BeginTransaction();                    cmd.Transaction = tran;                    //记录当前事务                    tranResult.Add(keyname,tran);                    //记录当前conn                    connResult.Add(keyname,conn);                    //读取当前conn的sql,执行                    foreach (string[] sql in sqlStrings)                    {                        if (sql[0] == keyname)                        {                            cmd.CommandText = sql[1];                            cmd.ExecuteNonquery();                        }                    }                }                catch(Exception ex)                {                    isSuccess = false;                }                if (!isSuccess)                {                    break;                }            }        }        //如果当前事务失败,把执行过的所有事务对象rollBack        if (!isSuccess)        {            foreach (sqlTransaction sqlTran in tranResult.Values)            {                sqlTran.Rollback();            }            reval = false;        }        else        {            foreach (sqlTransaction sqlTran in tranResult.Values)            {                sqlTran.Commit();            }        }        //关闭conn        foreach (sqlConnection value in connResult.Values)        {            if (value.State != ConnectionState.Closed)            {                value.Close();            }        }        return reval;    }    public sqlConnection CreateConnection(string keyname)    {        sqlConnection sqlconn = new sqlConnection(ConfigurationManager.AppSettings[keyname].ToString());        return sqlconn;    }

调用

//1.扣减玩家拍币数    //2.增加玩家保险柜金豆数    //3.插入拍币兑换记录    private bool procdata(int UserID,int pats,int ConvertRate,string ClIEntIP)    {        List<string[]> sqls = new List<string[]>();        //1.扣减玩家拍币数        string sql = "update AccountsInfo set UserPat=UserPat-" + pats + " where UserID=" + UserID;        sqls.Add(new string[] { "DBAccounts",sql });        //2.增加玩家保险柜金豆数        sql = "update GamescoreInfo set Insurescore=Insurescore+" + pats * ConvertRate + " where UserID=" + UserID;        sqls.Add(new string[] { "DBTreasure",sql });        //3.插入拍币兑换记录        sql = "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",";        sql += pats + ",";        sql += ConvertRate + ",";        sql += "1,";        sql += "" + ClIEntIP + "‘,";        sql += "" + System.DateTime.Now.ToString() + "";        sql += ")";        sqls.Add(new string[] { "DBRecord",sql });        return gData.ExecuteMultiTran(sqls);    }
总结

以上是内存溢出为你收集整理的sqlserver跨数据库事物同步(转vinsonLu)全部内容,希望文章能够帮你解决sqlserver跨数据库事物同步(转vinsonLu)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1173583.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存