作为一个完整的整体信息化解决方案需要有足够强大的各种功能,这些功能相对独立,又互相依存。当有需要这样的功能时可以随时拿出来用,适当修改一下就可以满足要求。只有这样才能快速开发各种信息化系统,才能满足各种客户的需求。
//-----------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2015 , Hairihan TECH, Ltd.
//-----------------------------------------------------------------
using System;
using System.Collections.Specialized;
using System.Data;
using System.Net;
using System.Text;
using Newtonsoft.Json;
namespace DotNet.Win
{
using DotNet.Business;
using DotNet.Utilities;
/// <summary>
/// 数据同步类
/// </summary>
public partial class Synchronous
{
/// <summary>
/// 远程数据同步的工具类
/// </summary>
/// <param name="fromDataBase">从服务器的哪个数据库获取数据</param>
/// <param name="tableName">同步哪个表</param>
/// <param name="primaryKeys">表的主键是什么</param>
/// <param name="modifiedOn">同步的更新时间</param>
/// <param name="toDataBaseDbType">同步到本地什么类型的数据库里?</param>
/// <param name="dbConnection">同步的目标数据库连接方式?</param>
/// <returns>影响行数</returns>
public static int SynchronousTable(string fromDataBase, string tableName, string[] primaryKeys, DateTime? modifiedOn, CurrentDbType toDataBaseDbType, string dbConnection)
{
int result = 0;
// 输入参数检查
if (primaryKeys == null)
{
return result;
}
int topLimit = 200;
DataTable dataTable = null;
IDbHelper dbHelper = null;
dbHelper = DbHelperFactory.GetHelper(toDataBaseDbType, dbConnection);
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
while (topLimit == 200)
{
string url = BaseSystemInfo.WebHost + "WebAPIV42/API/Synchronous/GetTopLimitTable";
WebClient webClient = new WebClient();
NameValueCollection postValues = new NameValueCollection();
postValues.Add("userInfo", BaseSystemInfo.UserInfo.Serialize());
postValues.Add("systemCode", BaseSystemInfo.SystemCode);
postValues.Add("securityKey", BaseSystemInfo.SecurityKey);
postValues.Add("dataBase", fromDataBase);
postValues.Add("tableName", tableName);
postValues.Add("topLimit", topLimit.ToString());
postValues.Add("modifiedOn", modifiedOn.Value.ToString(BaseSystemInfo.DateTimeFormat));
// 向服务器发送POST数据
byte[] responseArray = webClient.UploadValues(url, postValues);
string response = Encoding.UTF8.GetString(responseArray);
if (!string.IsNullOrEmpty(response))
{
dataTable = (DataTable)JsonConvert.DeserializeObject(response, typeof(DataTable));
}
int r = 0;
// 出错的日志都需要能保存起来,这样有问题的可以找出原因来。
for (r = 0; r < dataTable.Rows.Count; r++)
{
// 先删除数据,修改的、新增的、都删除后添加来处理,问题就简单化了
// dbHelper.ExecuteNonQuery("DELETE FROM " + tableName + " WHERE " + primaryKey + " = '" + dataTable.Rows[r][primaryKey].ToString() + "'");
sqlBuilder.BeginDelete(tableName);
for (int i = 0; i < primaryKeys.Length; i++)
{
string primaryKey = primaryKeys;
if (string.IsNullOrWhiteSpace(primaryKey))
{
sqlBuilder.SetWhere(primaryKey, dataTable.Rows[r][primaryKey].ToString());
}
}
sqlBuilder.EndDelete();
// 然后插入数据
sqlBuilder.BeginInsert(tableName);
for (int i = 0; i < dataTable.Columns.Count; i++)
{
// 这里能判断目标表里是否有这个字段存在就更完美了。
sqlBuilder.SetValue(dataTable.Columns.ColumnName, dataTable.Rows[r][dataTable.Columns.ColumnName]);
}
sqlBuilder.EndInsert();
if (DateTime.Parse(dataTable.Rows[r][BaseBusinessLogic.FieldModifiedOn].ToString()) > modifiedOn.Value)
{
modifiedOn = DateTime.Parse(dataTable.Rows[r][BaseBusinessLogic.FieldModifiedOn].ToString());
}
result++;
}
topLimit = dataTable.Rows.Count;
}
return result;
}
}
}