两个库同步更新数据库
我做的是2个数据库的一个备份同步更新,因为备份数据库有字段的冗余增加,所以不能完全BulkCopy这样的同步,需要进行一个修改再同步更新,具体修改代码不展示出来了,下面贴更新代码。
开始的时候,使用如下代码进行更新:
using (var conn = new SqlConnection(fromConn)) { if (conn.State != ConnectionState.Open) conn.Open(); using (var cmd = new SqlCommand()) { try { foreach (var item in sqlList) { var sql = item.Value; var tableName = item.Key; sw.Restart(); //获取表最后同步时间 var lastTime = GetLastTime(toConn, tableName, "update"); var nowTime = DateTime.Now;//当前同步时间 cmd.CommandText = string.Format(sql, lastTime, nowTime); cmd.CommandType = CommandType.Text; cmd.Connection = conn; cmd.CommandTimeout = 9999999; DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); if (dt.Rows.Count == 0) continue;//如果没有新增数据,跳过 foreach (DataRow row in dt.Rows) { row.SetModified(); } int updateCount = 0; using (var TmpConn = new SqlConnection(toConn)) { if (TmpConn.State != ConnectionState.Open) TmpConn.Open(); using (var tmpCmd = new SqlCommand()) { tmpCmd.CommandText = "select * from " + tableName; tmpCmd.CommandType = CommandType.Text; tmpCmd.Connection = TmpConn; SqlDataAdapter tmpSda = new SqlDataAdapter(tmpCmd); SqlCommandBuilder scb = new SqlCommandBuilder(tmpSda); tmpSda.UpdateCommand = scb.GetUpdateCommand(); updateCount = tmpSda.Update(dt); } } sw.Stop(); //修改当前表最后同步时间 UpdateLastTime(toConn, tableName, nowTime, "update"); Console.WriteLine("增量同步数据更新完成,表名:{0},数据{1}条,耗时{2}秒", tableName, updateCount, sw.Elapsed.TotalSeconds); } } catch (Exception e) { Console.WriteLine("增量同步数据同步更新失败," + e.ToString()); } } }
这种方法是没有效果的,因为这是2个数据库的表进行同步更新,SqlDataAdapter的Update只对同一个库有效(我只能做到这种,能不能2个库我现在做不出来),然后网上找另一种方法,然后有如下代码对数据库进行同步更新操作:
using (var conn = new SqlConnection(fromConn)) { if (conn.State != ConnectionState.Open) conn.Open(); using (var cmd = new SqlCommand()) { try { foreach (var item in sqlList) { var sql = item.Value; var tableName = item.Key; sw.Restart(); //获取表最后同步时间 var lastTime = GetLastTime(toConn, tableName, "update"); var nowTime = DateTime.Now;//当前同步时间 cmd.CommandText = string.Format(sql, lastTime.ToString("yyyy-MM-dd HH:mm:ss"), nowTime.ToString("yyyy-MM-dd HH:mm:ss")); cmd.CommandType = CommandType.Text; cmd.Connection = conn; cmd.CommandTimeout = 9999999; DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); if (dt.Rows.Count == 0) continue;//如果没有新增数据,跳过 foreach (DataRow row in dt.Rows) { row.SetModified(); } int updateCount = 0; using (var TmpConn = new SqlConnection(toConn)) { if (TmpConn.State != ConnectionState.Open) TmpConn.Open(); using (var tmpCmd = new SqlCommand()) { tmpCmd.CommandText = "select * from " + tableName; tmpCmd.CommandType = CommandType.Text; tmpCmd.Connection = TmpConn; SqlDataAdapter tmpSda = new SqlDataAdapter(tmpCmd); var columnList = new Dictionary<string, SqlDbType>(); var tmpColumnsql = "update Meb_" + tableName + " set "; var isone = true; foreach (DataColumn column in dt.Columns) { var cname = column.ToString(); columnList.Add(cname, GetSqlType(column.DataType)); if (cname.ToLower() == "id") continue; if (!isone) tmpColumnsql += ","; tmpColumnsql += cname + "=@" + cname; isone = false; } tmpColumnsql += " where Id=@Id"; tmpSda.UpdateCommand = new SqlCommand(tmpColumnsql, TmpConn); foreach (var column in columnList) { tmpSda.UpdateCommand.Parameters.Add("@" + column.Key, column.Value, int.MaxValue, column.Key); } tmpSda.UpdateCommand.UpdatedRowSource = UpdateRowSource.None; tmpSda.UpdateBatchSize = 500; updateCount = tmpSda.Update(dt); } } sw.Stop(); //修改当前表最后同步时间 UpdateLastTime(toConn, tableName, nowTime, "update"); Console.WriteLine("增量同步数据更新完成,表名:{0},数据总数:{1}条,耗时{2}秒", tableName, updateCount, sw.Elapsed.TotalSeconds); } } catch (Exception e) { Console.WriteLine("增量同步数据同步更新失败," + e.ToString()); } } }
这种方法能进行同步更新,但是性能现在还正在进行测试,不知道会不会很慢,因为数据量大的情况下,同步更新也是一个耗时的操作。
如果有更好的方法,求分享。