权限管理、用户权限系统、开源用户权限系统、信息化建设标准基础数据管理平台
代码改变世界

C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 远程同步服务器大量基础数据到客户端

2014-04-11 12:09  通用C#系统架构  阅读(3485)  评论(2编辑  收藏  举报

   服务器上保存有上万条的基础数据,需要同步到全国各地的成千上万个客户端,而且这些基础数据也经常在有变化调整。这时候需要有一个稳定的数据同步程序,能分批同步基础数据,由于网络流量,网络的稳定性等因素,需要分批读取有更新过的数据,花费了半天时间写了一个例子代码,客户端同步从服务器上同步基础数据的功能,可以提供给大家参考一下,希望能有重复利用的价值。

            // 1:打开业务数据库
            SqLiteHelper dbHelper = new SqLiteHelper(BaseSystemInfo.BusinessDbConnection);
            // 2: 先保存个同步时间标志,什么时间成功同步过本地数据库?这样不用每次都同步所有的数据,只同步那个时间之后的数据就可以了。
            BaseParameterManager parameterService = new BaseParameterManager(dbHelper, BaseSystemInfo.UserInfo);
            // 3:用参数的方式读取同步时间
            string synchronous = parameterService.GetParameter("System", BaseOrganizeEntity.TableName, "Synchronous");
            // 4: 传递参数用的,为了防注入漏洞的
            List<KeyValuePair<string, object>> dbParameters = new List<KeyValuePair<string, object>>();
            // 5:只获取有效的,未被删除的数据,(数据有被删除,也应该能同步才可以)
            string whereConditional = string.Empty;
            // string whereConditional = BaseOrganizeEntity.FieldDeletionStateCode + " = 0 AND " + BaseOrganizeEntity.FieldEnabled + " = 1 ";
            int recordCount = 0;
            int pageIndex = 1;
            // 网络情况好,客户端少,每次多获取一些数据,网络情况糟糕每次少获取一些数据
            int pageSize = 500;
            // 6:不需要获取所有数据,在最大程度上减少网络上的数据流量
            string selectField = BaseOrganizeEntity.FieldId + ","
                                 + BaseOrganizeEntity.FieldCode + ","
                                 + BaseOrganizeEntity.FieldFullName + ","
                                 + BaseOrganizeEntity.FieldEnabled + ","
                                 + BaseOrganizeEntity.FieldDeletionStateCode;
                                 
            int pageCount = 1;
            if (!string.IsNullOrEmpty(synchronous))
            {
                DateTime modifiedOn = DateTime.Parse(synchronous);
                dbParameters.Add(new KeyValuePair<string, object>(BaseOrganizeEntity.FieldModifiedOn, modifiedOn));
                if (!string.IsNullOrEmpty(whereConditional))
                {
                    whereConditional += " AND ";
                }
                // 7:只获取上次同步之后修改过的数据
                whereConditional += BaseOrganizeEntity.FieldModifiedOn + " > " + DbHelper.GetParameter(BaseOrganizeEntity.FieldModifiedOn);
            }
            // 8:调用远程服务,获取需要同步的数据
            DotNetService dotNetService = new DotNetService();
            DataTable dtSynchronous = dotNetService.UserCenterDbHelperService.GetDataTableByPage(BaseSystemInfo.UserInfo, out recordCount, BaseOrganizeEntity.TableName, selectField, pageIndex, pageSize, whereConditional, dbParameters, BaseOrganizeEntity.FieldId);
            // 9:防止网络连接超时,把网络连接释放掉
            if (dotNetService.UserCenterDbHelperService is ICommunicationObject)
            {
                ((ICommunicationObject)dotNetService.UserCenterDbHelperService).Close();
            }
            // 取得总页数
            if (recordCount % pageSize == 0)
            {
                pageCount = recordCount / pageSize;
            }
             else
            {
                pageCount = recordCount / pageSize + 1;
            }
            BaseOrganizeManager organizeManager = null;
            // 10:按每页进行循环获取,也防止数据库连接超时,网络连接超时,每次获取一部分数据
            while (dtSynchronous != null && dtSynchronous.Rows.Count > 0)
            {
                BaseOrganizeEntity organizeEntity = null;
                // 11:防止本地数据库连接超时,每次重新打开一个数据库连接,这样保险一些
                organizeManager = new BaseOrganizeManager(dbHelper, BaseSystemInfo.UserInfo);
                foreach (DataRow dr in dtSynchronous.Rows)
                {
                    // 12:不用实体化其实也可以的,先实体化吧,代码规范一些
                    organizeEntity = new BaseOrganizeEntity();
                    organizeEntity.Id = int.Parse(dr[BaseOrganizeEntity.FieldId].ToString());
                    organizeEntity.Code = dr[BaseOrganizeEntity.FieldCode].ToString();
                    organizeEntity.FullName = dr[BaseOrganizeEntity.FieldFullName].ToString();
                    organizeEntity.Enabled = int.Parse(dr[BaseOrganizeEntity.FieldEnabled].ToString());
                    organizeEntity.DeletionStateCode = int.Parse(dr[BaseOrganizeEntity.FieldDeletionStateCode].ToString());
                    // 13: 先按更新操作。
                    SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
                    sqlBuilder.BeginUpdate(BaseOrganizeEntity.TableName);
                    sqlBuilder.SetValue(BaseOrganizeEntity.FieldCode, organizeEntity.Code);
                    sqlBuilder.SetValue(BaseOrganizeEntity.FieldFullName, organizeEntity.FullName);
                    sqlBuilder.SetValue(BaseOrganizeEntity.FieldEnabled, organizeEntity.Enabled);
                    sqlBuilder.SetValue(BaseOrganizeEntity.FieldDeletionStateCode, organizeEntity.DeletionStateCode);
                    sqlBuilder.SetWhere(BaseOrganizeEntity.FieldId, organizeEntity.Id);
                    if (sqlBuilder.EndUpdate() == 0)
                    {
                        // 14: 若没找到数据,进行插入操作。
                        sqlBuilder.BeginInsert(BaseOrganizeEntity.TableName);
                        sqlBuilder.SetValue(BaseOrganizeEntity.FieldId, organizeEntity.Id);
                        sqlBuilder.SetValue(BaseOrganizeEntity.FieldCode, organizeEntity.Code);
                        sqlBuilder.SetValue(BaseOrganizeEntity.FieldFullName, organizeEntity.FullName);
                        sqlBuilder.SetValue(BaseOrganizeEntity.FieldEnabled, organizeEntity.Enabled);
                        sqlBuilder.SetValue(BaseOrganizeEntity.FieldDeletionStateCode, organizeEntity.DeletionStateCode);
                        sqlBuilder.EndInsert();
                    }
                    // 15: 这个代码是按实体进行更新同步的代码例子。
                    // 若有缺少字段什么的,也闹心
                    // if (organizeManager.UpdateObject(organizeEntity) == 0)
                    // {
                    //    organizeManager.AddObject(organizeEntity);
                    // }
                }
                // 16: 看是否有必要继续获取数据,判断页数,若没必要能少循环一次就少循环一次
                dtSynchronous = null;
                if (pageIndex < pageCount)
                {
                    // 17: 若数据没获取好,继续获取数据
                    pageIndex++;
                    dotNetService = new DotNetService();
                    dtSynchronous = dotNetService.UserCenterDbHelperService.GetDataTableByPage(BaseSystemInfo.UserInfo, out recordCount, BaseOrganizeEntity.TableName, selectField, pageIndex, pageSize, whereConditional, dbParameters, BaseOrganizeEntity.FieldId);
                    if (dotNetService.UserCenterDbHelperService is ICommunicationObject)
                    {
                        ((ICommunicationObject)dotNetService.UserCenterDbHelperService).Close();
                    }
                }
            }
            // 18: 更新数据库中的同步时间标志。
            parameterService.SetParameter("System", BaseOrganizeEntity.TableName, "Synchronous", DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat));

 



C# ASP.NET 通用权限设计、通用权限管理、通用权限组件、单点登录、集中式权限管理、统一授权体系、分级管理分级授权


微信扫一扫加好友