日期:2014-05-18 浏览次数:21215 次
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); } } } } }
/// <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);