public int SyncGlobalTable(string tablename) { #region [Sync Global Table to Local Table] int status = 0; if (!string.IsNullOrEmpty(tablename)) { //1.得到中央服务器My SQL库中的表对应的DataTable DataTable globalDT = GetGlobalDataTable(tablename); //2.删除原表中的所有数据 sql = string.Format(@"delete from {0}", tablename); DbFactory.executeNonQuery(sql); //3.得到自己库中表对应的DataTable, sql=string.Format(@"select * from {0}",tablename); DataTable selfDT = DbFactory.getDataTableBySql(sql); //4.清空所有行,所有列置为可空,可写 selfDT.Rows.Clear(); foreach (DataColumn dcS in selfDT.Columns) { dcS.AllowDBNull = true; dcS.ReadOnly = false; dcS.AutoIncrement = false; } //5.将中央服务器上的globalDT中的数据赋给自己库对应的selfDT foreach (DataRow dr in globalDT.Rows) { DataRow tempDr = selfDT.Rows.Add(); foreach (DataColumn dc in globalDT.Columns) { string colName = dc.ColumnName; tempDr[colName] = dr[colName]; } } SqlDataAdapter da = new SqlDataAdapter(sql, DbFactory.getConnection()); SqlCommandBuilder cb = new SqlCommandBuilder(da); status = da.Update(selfDT); } return status; #endregion }
同步更新DataTable
最新推荐文章于 2020-05-22 19:11:28 发布