日期:2014-05-18  浏览次数:21215 次

关于WCF客户端与服务端数据事务同步的问题!急……
客户端经常掉线,我就想着用WCF事务来做数据同步,现在服务器数据库是ORACLE,客户端数据库是SQLSERVER,我写了一个测试程序,并网数据库里面导入10000条数据,数据同步的时候时不时的拔下网线,结果同步的数据却不一致,废话不多说,直接上代码,请各位大侠指正看哪里不对导致数据不能同步?

客户端执行代码如下:

C# code
public override void mySynchronous()
        {
            int tableCount = GetTableCount();
            string TableCountMessage = string.Format("当前共有未同步数据{0}条!", tableCount);
            Console.WriteLine(TableCountMessage);
            if (tableCount > 0)
            {
                Console.WriteLine("开始同步数据……");
                MyClientService.HelloServiceClient service = new MyClientService.HelloServiceClient();
                System.Transactions.TransactionOptions tran = new System.Transactions.TransactionOptions();
                //设置事务超时时间
                tran.Timeout = new TimeSpan(3000);
                //设置事务的隔离级别
                tran.IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead;
                DataTable myDataTable = GetUserDataTable();
                foreach (DataRow dr in myDataTable.Rows)
                {
                    using (var ts = new System.Transactions.TransactionScope())
                    {
                        try
                        {
                            string name = dr["testName"].ToString();
                            string id = dr["userID"].ToString();
                            if (service.InserOraclData(name, id))
                            {
                                updateType(id);
                            }
                            else
                            {
                                throw new Exception("服务端插入错误!");
                            }

                            //service.TransactMyServer(myDataTable);
                            Console.WriteLine(string.Format("{0}数据ID:{1}数据更新成功!", DateTime.Now.ToString(), id));
                            ts.Complete();


                        }
                        catch (Exception ex)
                        {

                            Console.WriteLine(DateTime.Now.ToString() + "数据更新失败!失败原因如下:");
                            Console.WriteLine(ex.Message);
                            Transaction.Current.Rollback(ex);

                        }

                    }

                }
            }
        
        }

服务端InseroraclData方法
C# code

  /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true )]
        public bool  InserOraclData(string name, string id)
        {
            string sql = @"INSERT INTO Test1
                                       (testName
                                       ,TYPE
                                       ,userID)
                                 VALUES
                                       (:testName
                                       ,'1'
                                       ,:userID)";
            OracleConnection mycon = myOracleServerData.myConn;
            OracleCommand mycmd = new OracleCommand(sql,mycon);
            mycmd.Parameters.Add(new OracleParameter(":testName",name));
            mycmd.Parameters.Add(new OracleParameter(":userID",id));
            try
            {
                mycon.Open();
                mycmd.ExecuteNonQuery();
                return true;
            }
            catch (OracleException ex)
            {
                Console.WriteLine(ex);