/// <summary> /// 导出成功后,更新实体表的导出批次号 /// </summary> /// <param name="entity"></param> /// <param name="batchNumber"></param> /// <returns></returns> /// <remarks>已经在导出前更新了批次号,此方法已经废弃</remarks> private int UpdateBatchNumber(EntityBase entity, int batchNumber) { if (entity is IExportTable) { OQL q = null; entity[C_BatchNumber] = batchNumber; try { //如果AuditworkProjectID 属性不存在,那么此时赋值会出错 AuditworkProjectID entity[C_Classification] = ClassificationID; q = OQL.From(entity) .Update(entity[C_BatchNumber]) .Where(cmp => cmp.EqualValue(entity[C_Classification]) & (cmp.Comparer(entity[C_BatchNumber], "=", 0) | cmp.IsNull(entity[C_BatchNumber])) ) .END; } catch (Exception ex) { q = OQL.From(entity) .Update(entity[C_BatchNumber]) .Where(cmp => cmp.Comparer(entity[C_BatchNumber], "=", 0) | cmp.IsNull(entity[C_BatchNumber])) .END; } int count = EntityQuery.ExecuteOql(q, CurrDbContext.CurrentDataBase); return(count); } return(0); }
/// <summary> /// 快速删除数据,内部根据实体类的主键进行删除 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> public void QuickDelete <T>(List <T> list) where T : EntityBase, new() { List <object> idList = new List <object>(); T entity0 = list[0]; string tableName = entity0.GetTableName(); string pkName = entity0.PrimaryKeys[0]; foreach (T entity in list) { idList.Add(entity[pkName]); } List <DeletedPKIDEntity> dteList = new List <DeletedPKIDEntity>(); foreach (T entity in list) { object pkValue = entity[pkName]; DeletedPKIDEntity idEntity = new DeletedPKIDEntity(); if (pkValue is string) { idEntity.TargetID = 0; idEntity.TargetStringID = pkValue.ToString(); } else { idEntity.TargetID = Convert.ToInt64(pkValue); idEntity.TargetStringID = ""; } idEntity.TargetTableName = tableName; idEntity.DeletedTime = DateTime.Now; dteList.Add(idEntity); } OQL deleteQ = OQL.From(entity0) .Delete() .Where(cmp => cmp.Comparer(entity0[pkName], OQLCompare.CompareType.IN, idList.ToArray())) .END; AdoHelper db = this.OptDbContext.CurrentDataBase; EntityQuery <DeletedPKIDEntity> eq = new EntityQuery <DeletedPKIDEntity>(db); db.BeginTransaction(); try { int delCount = eq.ExecuteOql(deleteQ); int insCount = eq.QuickInsert(dteList); if (delCount != insCount) { throw new Exception("插入和删除的数据记录数不一样,删除数:" + delCount + ",插入数:" + insCount); } db.Commit(); } catch (Exception ex) { db.Rollback(); } }
/// <summary> /// 删除 /// </summary> /// <param name="nodeCodes"></param> public void DeleteBaseConfigs(string nodeCodes) { try { //开始事物 db_dataReportWJW.BeginTransaction(); FgSys_BaseConfig entity = new FgSys_BaseConfig(); string[] nodeCodeArray = nodeCodes.Split(','); for (int i = 0; i < nodeCodeArray.Length; i++) { OQLCompareFunc <FgSys_BaseConfig> cmpFun = (cmp, u) => { OQLCompare cmpResult = null; cmpResult = cmpResult & cmp.Comparer(entity.NodeCode, OQLCompare.CompareType.Equal, nodeCodeArray[i]); return(cmpResult); }; OQL deleteQ = OQL.From(entity).Delete().Where(cmpFun).END; EntityQuery <FgSys_BaseConfig> .ExecuteOql(deleteQ, db_dataReportWJW); } //提交事物 db_dataReportWJW.Commit(); } catch (Exception err) { //回滚事物 db_dataReportWJW.Rollback(); throw err; } }
private void Exporter_OnExported <T>(object sender, ExportEntityEventArgs <T> e) where T : EntityBase, new() { Console.WriteLine("------------------------------------------------------------"); if (e.Succeed) { //处理上次没有导入的剩余数据============================ //尝试加载本地数据,与导出的数据合并,以本次导出的数据优先 List <T> lastData = this.MemDB.LoadEntity <T>(); if (lastData != null && lastData.Count > 0) { string pkName = lastData[0].PrimaryKeys[0]; if (e.ExportedDataList.Count > 0) { object[] ids = e.ExportedDataList.Select(p => p[pkName]).ToArray(); var except = lastData.Where(p => !ids.Contains(p[pkName])).ToList(); e.ExportedDataList.AddRange(except); } else { e.ExportedDataList.AddRange(lastData); } } if (e.ExportedDataList.Count > 0) { if (e.EntityType == typeof(DeletedPKIDEntity)) { //ID记录表,导出后删除当前批次数据库记录 DeletedPKIDEntity entity = new DeletedPKIDEntity(); OQL q = OQL.From(entity) .Delete() .Where(cmp => cmp.Comparer(entity.BatchNumber, "=", e.BatchNumber)) .END; int count = EntityQuery <DeletedPKIDEntity> .ExecuteOql(q, CurrDbContext.CurrentDataBase); Console.WriteLine("当前导出批次{0} 已经清除当前的ID删除表信息记录,条数:{1}", e.BatchNumber, count); } else { //已经提前更新了导出批次号,参见 FilterQuery 方法 } this.HaveDataTableCount++; } Console.WriteLine("导出数据成功!\t 导出批次号:{0}\t 导出表名称:{1}\t 导出记录数:{2}", e.BatchNumber, e.ExportTable, e.ExportedDataList.Count); } else { Console.WriteLine("导出数据失败,\r\n 导出批次号:{0}\r\n 导出表名称:{1}\r\n 出错原因:{2}", e.BatchNumber, e.ExportTable, e.OperationExcepiton.Message); this.AllSucceed = false; } }
/// <summary> /// 修改 /// </summary> /// <param name="nodeCodes"></param> public void UpdateBaseConfig(FgSys_BaseConfig entity) { try { OQL updateQ = OQL.From(entity).Delete().Where(entity.NodeCode).END; EntityQuery <FgSys_BaseConfig> .ExecuteOql(updateQ, db_dataReportWJW); } catch (Exception err) { throw err; } }
public static bool DeleteB <T>(string IDs) where T : EntityBase, IStringID, new() { string[] strs = IDs.Split(new char[] { ',' }); T t = new T(); OQL q = OQL.From(t).Delete().Where <T>((cmp1, user) => cmp1.Comparer(user.ID.ToString(), "in", strs)).END; try { return(EntityQuery <T> .ExecuteOql(q, MyDB.GetDBHelper()) > 0); } catch { return(false); } }
/// <summary> /// 生成批次号为空或者为0的查询,对应于本地新增或者更新过的数据 /// </summary> /// <param name="batchNumber">批次号</param> /// <param name="entity"></param> /// <returns></returns> private OQL FilterQuery(int batchNumber, EntityBase entity) { if (entity is IExportTable) { var tableInfoCache = EntityFieldsCache.Item(entity.GetType()); var fieldList = tableInfoCache.PropertyNames; Console.WriteLine("===================导出表 [{0}] ==================", entity.GetTableName()); IExportTable ExportableEntity = entity as IExportTable; ExportableEntity.BatchNumber = batchNumber; //将数据库导出标记为0或者为空的记录,更新为当前导出标记号 //一定得按数据分类标识更新及导出 OQL updateQ = null; if (fieldList.Contains(C_Classification)) { entity[C_Classification] = ClassificationID; updateQ = OQL.From(entity) .Update(ExportableEntity.BatchNumber) .Where(cmp => cmp.EqualValue(entity[C_Classification]) & (cmp.Comparer(ExportableEntity.BatchNumber, "=", 0) | cmp.IsNull(ExportableEntity.BatchNumber)) ) .END; int count = EntityQuery.ExecuteOql(updateQ, this.CurrDbContext.CurrentDataBase); Console.WriteLine("(查询前)更新批次号 {0} 受影响的记录数 {1}", batchNumber, count); OQL q = OQL.From(entity) .Select() .Where(cmp => cmp.EqualValue(entity[C_Classification]) & cmp.EqualValue(ExportableEntity.BatchNumber)) .END; return(q); } else { updateQ = OQL.From(entity) .Update(ExportableEntity.BatchNumber) .Where(cmp => cmp.Comparer(ExportableEntity.BatchNumber, "=", 0) | cmp.IsNull(ExportableEntity.BatchNumber)) .END; int count = EntityQuery.ExecuteOql(updateQ, this.CurrDbContext.CurrentDataBase); Console.WriteLine("(查询前)更新批次号 {0} 受影响的记录数 {1}", batchNumber, count); OQL q = OQL.From(entity) .Select() .Where(cmp => cmp.EqualValue(ExportableEntity.BatchNumber)) .END; return(q); } } return(null); }
public static int SetDeleteB <T>(string IDs, AdoHelper ado) where T : EntityBase, IStringID, IIsDelete, new() { string[] strs = IDs.Split(new char[] { ',' }); T t = new T() { IsDelete = true }; OQL q = OQL.From(t).Update(t.IsDelete).Where <T>((cmp1, u) => cmp1.Comparer(u.ID, "in", strs)).END; try { return(EntityQuery <T> .ExecuteOql(q, ado)); } catch { return(0); } }
public static int SetDelete <T>(string IDs) where T : EntityBase, IIntID, IIsDelete, new() { string[] strs = IDs.Split(new char[] { ',' }); T t = new T() { IsDelete = true }; OQL.From(t).Select(t.ID, t.IsDelete); OQL q = OQL.From(t).Update(t.IsDelete).Where <T>((cmp1, user) => cmp1.Comparer(user.ID.ToString(), "in", strs)).END; try { return(EntityQuery <T> .ExecuteOql(q, MyDB.GetDBHelper())); } catch { return(0); } }
static void Main(string[] args) { Console.WriteLine("====**************** PDF.NET SOD ORM 控制台测试程序 **************===="); Assembly coreAss = Assembly.GetAssembly(typeof(AdoHelper));//获得引用程序集 Console.WriteLine("框架核心程序集 PWMIS.Core Version:{0}", coreAss.GetName().Version.ToString()); Console.WriteLine(); Console.WriteLine(@"====应用程序配置文件默认的数据库配置信息:=========================== 当前使用的数据库类型是:{0} 连接字符串为:{1} 请确保数据库服务器和数据库是否有效(SqlServer,Access 会自动创建数据库), 继续请回车,退出请输入字母 Q ." , MyDB.Instance.CurrentDBMSType.ToString(), MyDB.Instance.ConnectionString); Console.WriteLine("=====Power by Bluedoctor,2015.3.1 http://www.pwmis.com/sqlmap ======="); string read = Console.ReadLine(); if (read.ToUpper() == "Q") { return; } Console.WriteLine(); Console.WriteLine("-------PDF.NET SOD ORM 测试 开始 ---------"); //自动创建数据库和表 LocalDbContext context = new LocalDbContext(); int count = 0; //测试查询&&更新 User zhang_san = new User() { Name = "zhang san" }; User dbZhangSan = EntityQuery <User> .QueryObject( OQL.From(zhang_san) .Select(zhang_san.ID, zhang_san.Name, zhang_san.Pwd) .Where(cmp => cmp.EqualValue(zhang_san.Name)) .END ); if (dbZhangSan != null) { dbZhangSan.Pwd = "111111"; dbZhangSan.RegistedDate = DateTime.Now; int updateCount = EntityQuery <User> .Instance.Update(dbZhangSan); Console.WriteLine("测试:用户{0} 的密码和注册日期已经更新", zhang_san.Name); } else { Console.WriteLine("测试:用户{0} 的记录不存在,如果需要测试查询和更新,请先添加一条记录。继续测试,请按任意键。", zhang_san.Name); Console.Read(); } System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //删除 测试数据----------------------------------------------------- User user = new User(); OQL deleteQ = OQL.From(user) .Delete() .Where(cmp => cmp.Comparer(user.ID, ">", 0)) //为了安全,不带Where条件是不会全部删除数据的 .END; count = EntityQuery <User> .ExecuteOql(deleteQ, context.CurrentDataBase); Console.WriteLine("--删除 {0}条数据--", count); //----------------------------------------------------------------- //插入 测试数据----------------------------------------------------- count = 0; zhang_san = new User() { Name = "zhang san", Pwd = "123" }; count += context.Add <User>(zhang_san);//采用 DbContext 方式插入数据 User li_si = new User() { Name = "li si", Pwd = "123" }; OQL insertQ = OQL.From(li_si) .Insert(li_si.Name); //仅仅插入用户名,不插入密码 //采用OQL方式插入指定的数据 //同时演示事务使用方法 AdoHelper db = MyDB.GetDBHelperByConnectionName("local"); EntityQuery <User> userQuery = new EntityQuery <User>(db); //db.BeginTransaction(); //try //{ // count += userQuery.ExecuteOql(insertQ); // // userQuery.GetInsertIdentity(insertQ); 获取插入标识,用词方法代替下面的方式 // //OQL 方式没法取得自增数据,所以下面单独查询 // object obj_id = db.ExecuteScalar(db.InsertKey); // db.Commit(); // li_si.ID = Convert.ToInt32( obj_id); //} //catch (Exception ex) //{ // Console.WriteLine("AdoHelper 执行事务异常:"+ex.Message ); // db.Rollback(); // Console.WriteLine("按任意键退出!"); // return; //} //上面的代码注释,采用下面封装的代码:ExecuteInsrtOql li_si.ID = userQuery.ExecuteInsrtOql(insertQ); List <User> batchList = new List <User>(); for (int i = 0; i < 1000; i++) { User zhang_yeye = new User() { ID = 1000 + i, Name = "zhang yeye" + i, Pwd = "pwd" + i }; //count += EntityQuery<User>.Instance.Insert(zhang_yeye);//采用泛型 EntityQuery 方式插入数据 batchList.Add(zhang_yeye); } watch.Stop(); Console.WriteLine("耗时:(ms)" + watch.ElapsedMilliseconds); watch.Restart(); count = EntityQuery <User> .Instance.QuickInsert(batchList); watch.Stop(); Console.WriteLine("QuickInsert List 耗时:(ms)" + watch.ElapsedMilliseconds); watch.Restart(); count = EntityQuery <User> .Instance.Insert(batchList); watch.Stop(); Console.WriteLine("Insert List 耗时:(ms)" + watch.ElapsedMilliseconds); Console.WriteLine("--插入 {0}条数据--", count); //----------------------------------------------------------------- //修改 测试数据---------------------------------------------------- li_si.Pwd = "123123"; count = context.Update <User>(li_si);//采用 DbContext 方式更新数据 li_si.Pwd = "123456"; OQL updateQ = OQL.From(li_si) .Update(li_si.Pwd) //仅仅更新密码 .END; count += EntityQuery <User> .Instance.ExecuteOql(updateQ);//采用OQL方式更新指定的数据 li_si.Pwd = "888888"; count += EntityQuery <User> .Instance.Update(li_si);//采用泛型 EntityQuery 方式修改数据 Console.WriteLine("--修改 {0}次数据,User ID:{1}--", count, li_si.ID); //----------------------------------------------------------------- //查询数据--------------------------------------------------------- Console.WriteLine("SOD ORM的 6种 查询方式,开始----"); UserLoginService service = new UserLoginService(); Console.WriteLine("Login0:{0}", service.Login(zhang_san)); Console.WriteLine("Login1:{0}", service.Login1(zhang_san)); Console.WriteLine("Login2:{0}", service.Login2("zhang san", "123")); Console.WriteLine("Login3:{0}", service.Login3("zhang san", "123")); Console.WriteLine("Login4:{0}", service.Login4("zhang san", "123")); Console.WriteLine("Login5:{0}", service.Login5("zhang san", "123")); Console.WriteLine("Login6:{0}", service.Login6("zhang yeye", "pwd100")); //查询列表 var users = service.FuzzyQueryUser("zhang"); Console.WriteLine("模糊查询姓 张 的用户,数量:{0}", users.Count); //----------------------------------------------------------------- Console.WriteLine("-------PDF.NET SOD ORM 测试 全部结束----- "); watch.Stop(); Console.WriteLine("耗时:(ms)" + watch.ElapsedMilliseconds); Console.Read(); }
/// <summary> /// 导入数据到关系数据库 /// </summary> /// <param name="mode">导入模式</param> /// <param name="isNew">导入模式为更新模式的时候,进行实体类数据新旧比较的自定义方法,第一个参数为源实体,第二个参数为数据库的目标实体,返回源是否比目标新</param> /// <returns>导入的数据数量</returns> public ImportResult Import(ImportMode mode, Func <T, T, bool> isNew) { Type entityType = typeof(T); string importTableName = EntityFieldsCache.Item(entityType).TableName; ImportResult result = new ImportResult(); result.ImportTable = importTableName; result.IsCancel = true; //导出批次管理 string memDbPath = this.MemDB.Path; string pkgPath = memDbPath.Length > 255 ? memDbPath.Substring(memDbPath.Length - 255) : memDbPath; List <ExportBatchInfo> batchList = MemDB.Get <ExportBatchInfo>(); ExportBatchInfo currBatch = batchList.FirstOrDefault(p => p.ExportTableName == importTableName); if (currBatch == null) { result.Flag = ImportResultFlag.NoBatchInfo; return(result);//没有导入批次信息,不能导入 } //只有比数据库的导入批次数据新,才可以导入 currBatch.PackagePath = pkgPath; OQL q = OQL.From(currBatch) .Select() .Where(currBatch.ExportTableName, currBatch.PackagePath) .END; ExportBatchInfo dbBatch = this.CurrDbContext.QueryObject <ExportBatchInfo>(q); if (dbBatch == null) { currBatch.SetDefaultChanges(); this.CurrDbContext.Add <ExportBatchInfo>(currBatch); result.BatchNumber = currBatch.BatchNumber; } else { result.BatchNumber = currBatch.BatchNumber; if (currBatch.BatchNumber <= dbBatch.BatchNumber) { result.Flag = ImportResultFlag.IsOldData; return(result);//没有新数据需要导入 } currBatch.ID = dbBatch.ID; } //导入数据 int count = 0;// List <T> list = this.MemDB.Get <T>(); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); if (list.Count > 0) { ImportEntityEventArgs <T> args = new ImportEntityEventArgs <T>(list, entityType, importTableName, currBatch.BatchNumber); if (BeforeImport != null) { BeforeImport(this, args); if (args.Cancel) { result.Flag = ImportResultFlag.UserCanceled; return(result); } } //处理不同的导入模式 if (mode == ImportMode.Append) { list.ForEach(item => { item.ResetChanges(true); }); count = this.CurrDbContext.AddList(list); } else if (mode == ImportMode.TruncateAndInsert) { string sql = "TRUNCATE TABLE [" + importTableName + "]"; this.CurrDbContext.CurrentDataBase.ExecuteNonQuery(sql); //list.ForEach(item => //{ // item.SetDefaultChanges(); //}); //count = this.CurrDbContext.AddList(list); list[0].ResetChanges(true); EntityQuery <T> eq = new EntityQuery <T>(this.CurrDbContext.CurrentDataBase); count = eq.QuickInsert(list); } else if (mode == ImportMode.Update) { if (isNew == null) { throw new ArgumentNullException("当 ImportMode 为Update 模式的时候,参数 isNew 不能为空。"); } foreach (T item in list) { T dbEntity = (T)item.Clone(); EntityQuery eq = new EntityQuery(this.CurrDbContext.CurrentDataBase); if (eq.FillEntity(dbEntity)) { if (isNew(item, dbEntity)) { item.ResetChanges(true);; //设置了更改状态,才可以更新到数据库 count += eq.Update(item); } } } } else if (mode == ImportMode.Merge) { /* * //下面的方式比较缓慢,改用先删除数据包对应的数据再快速插入的方式 * foreach (T item in list) * { * T dbEntity = (T)item.Clone(); * EntityQuery eq = new EntityQuery(this.CurrDbContext.CurrentDataBase); * if (eq.FillEntity(dbEntity)) * { * int changedCount = dbEntity.MapFrom(item, true); * if (changedCount > 0) * { * count+= eq.Update(dbEntity); * } * } * else * { * //没有Fill成功实体,说明数据库没有此数据,则添加数据到数据库 * item.SetDefaultChanges(); * count+= eq.Insert(item); * } * } * // */ var idList = list.Select(s => s[s.PrimaryKeys[0]]).ToList(); //每页大小 const int pageSize = 500; //页码 int pageNum = 0; T entity = new T(); list[0].ResetChanges(true); EntityQuery <T> eq = new EntityQuery <T>(this.CurrDbContext.CurrentDataBase); this.CurrDbContext.CurrentDataBase.BeginTransaction(); try { while (pageNum * pageSize < idList.Count) { var currIdList = idList.Skip(pageSize * pageNum).Take(pageSize); var deleteQ = OQL.From(entity) .Delete() .Where(cmp => cmp.Comparer(entity[entity.PrimaryKeys[0]], "in", currIdList.ToArray())) .END; int deleteCount = eq.ExecuteOql(deleteQ); pageNum++; } count = eq.QuickInsert(list); this.CurrDbContext.CurrentDataBase.Commit(); } catch (Exception ex) { count = 0; this.CurrDbContext.CurrentDataBase.Rollback(); result.IsCancel = true; result.Flag = ImportResultFlag.Error; result.ImportCount = count; result.ErrorMessage = ex.Message; if (ex.InnerException != null) { QueryException qe = ex.InnerException as QueryException; if (qe != null) { result.ErrorMessage += ":QueryException :" + qe.Message; } else { result.ErrorMessage += ":Error :" + ex.InnerException.Message; } } return(result); } } else { //自定义的处理方式,请在 BeforeImport 事件自行处理 } if (AfterImport != null) { args.Cancel = false; AfterImport(this, args); } }//end if //更新导入批次信息 currBatch.BatchNumber = result.BatchNumber; currBatch.LastExportDate = DateTime.Now; this.CurrDbContext.Update <ExportBatchInfo>(currBatch); watch.Stop(); result.Duration = Convert.ToInt64(watch.Elapsed.TotalSeconds); result.IsCancel = false; result.Flag = ImportResultFlag.Succeed; result.ImportCount = count; return(result); }
/// <summary> /// 使用默认连接,执行OQL /// </summary> /// <param name="oql">OQL</param> /// <returns>操作受影响的行数</returns> public static int Execute(this OQL oql) { return(EntityQuery.ExecuteOql(oql, MyDB.Instance)); }
/// <summary> /// 执行OQL /// </summary> /// <param name="oql">OQL</param> /// <param name="db">数据访问对象</param> /// <returns>操作受影响的行数</returns> public static int Execute(this OQL oql, AdoHelper db) { return(EntityQuery.ExecuteOql(oql, db)); }
/// <summary> /// 根据ID删除表,删除目标表的数据 /// </summary> private void ExecuteDeleteData() { List <DeletedPKIDEntity> idList = this.MemDB.Get <DeletedPKIDEntity>(); var tableNames = idList.Select(s => s.TargetTableName).Distinct().ToArray(); List <EntityBase> entitys = CurrDbContext.ResolveAllEntitys(); foreach (string name in tableNames) { EntityBase targetEntity = entitys.FirstOrDefault(p => p.GetTableName() == name); if (targetEntity == null) { Console.WriteLine("表名称{0} 不属于实体类型集合中的任意一个元素", name); continue; } string pkName = targetEntity.PrimaryKeys[0]; var targetIDs = idList.Where(p => p.TargetTableName == name && p.TargetID > 0).Select(s => s.TargetID).ToArray(); Console.WriteLine("表{0} 可能有{1}条数据需要删除..", name, targetIDs.Length); OQL q = null; int DATA_BLOCK = 500;//必须限定in查询的数量,否则出错 if (targetIDs.Length > 0) { targetEntity[pkName] = 0; if (targetIDs.Length > DATA_BLOCK) { long[] arrIds = new long[DATA_BLOCK]; int offset = 0; while (offset < targetIDs.Length) { int length = offset + DATA_BLOCK < targetIDs.Length ? DATA_BLOCK : targetIDs.Length - offset; if (length < DATA_BLOCK) { //处理结尾的数据 arrIds = new long[length]; } Array.Copy(targetIDs, offset, arrIds, 0, length); //构造和执行查询 q = OQL.From(targetEntity) .Delete() .Where(cmp => cmp.Comparer(Convert.ToInt64(targetEntity[pkName]), "in", arrIds)) .END; int cur_count = EntityQuery.ExecuteOql(q, CurrDbContext.CurrentDataBase); Console.WriteLine("已经删除表{0} {1}条数据!", name, cur_count); offset += DATA_BLOCK; } } else { //构造和执行查询 q = OQL.From(targetEntity) .Delete() .Where(cmp => cmp.Comparer(Convert.ToInt64(targetEntity[pkName]), "in", targetIDs)) .END; int cur_count = EntityQuery.ExecuteOql(q, CurrDbContext.CurrentDataBase); Console.WriteLine("已经删除表{0} {1}条数据!", name, cur_count); } } else { //字符串ID处理 var targetStringIDs = idList.Where(p => p.TargetTableName == name).Select(s => s.TargetStringID).ToArray(); targetEntity[pkName] = "0"; // if (targetStringIDs.Length > DATA_BLOCK) { string[] arrIds = new string[DATA_BLOCK]; int offset = 0; while (offset < targetStringIDs.Length) { int length = offset + DATA_BLOCK < targetStringIDs.Length ? DATA_BLOCK : targetStringIDs.Length - offset; if (length < DATA_BLOCK) { //处理结尾的数据 arrIds = new string[length]; } Array.Copy(targetStringIDs, offset, arrIds, 0, length); //构造和执行查询 q = OQL.From(targetEntity) .Delete() .Where(cmp => cmp.Comparer((string)targetEntity[pkName], "in", arrIds)) .END; int cur_count = EntityQuery.ExecuteOql(q, CurrDbContext.CurrentDataBase); Console.WriteLine("已经删除表{0} {1}条数据!", name, cur_count); offset += DATA_BLOCK; } } else { //构造和执行查询 q = OQL.From(targetEntity) .Delete() .Where(cmp => cmp.Comparer((string)targetEntity[pkName], "in", targetStringIDs)) .END; int cur_count = EntityQuery.ExecuteOql(q, CurrDbContext.CurrentDataBase); Console.WriteLine("已经删除表{0} {1}条数据!", name, cur_count); } } } }
/// <summary> /// 保存销售信息 /// </summary> /// <param name="customer">客户信息</param> /// <param name="integral">要增加的积分</param> /// <returns></returns> private GoodsSellNote SaveSalesInfo(Customer customer, int integral) { GoodsSellNote note = new GoodsSellNote(); note.CustomerID = customer.CustomerID; note.ManchinesNumber = this.CurrCRManchines.CashRegisterNo; note.SalesmanID = this.CurrCashier.WorkNumber; note.SalesType = "店内销售"; note.SellDate = DateTime.Now; note.GoodsSellDetails = new List <GoodsSellDetail>(); AdoHelper db = MyDB.GetDBHelper(); db.BeginTransaction(); try { EntityQuery <GoodsSellNote> query = new EntityQuery <GoodsSellNote>(db); if (query.Insert(note) > 0) { foreach (Goods goods in customer.Goodss) { if (goods.GoodsNumber > 0) { //处理详单 GoodsSellDetail detail = new GoodsSellDetail(); detail.GoodsPrice = goods.GoodsPrice; detail.NoteID = note.NoteID; detail.SellNumber = goods.GoodsNumber; detail.SerialNumber = goods.SerialNumber; note.GoodsSellDetails.Add(detail); //更新库存 SuperMarketDAL.Entitys.GoodsStock stock = new SuperMarketDAL.Entitys.GoodsStock(); stock.GoodsID = goods.GoodsID; stock.Stocks = goods.GoodsNumber; OQL q = OQL.From(stock) .UpdateSelf('-', stock.Stocks) .Where(stock.GoodsID) .END; EntityQuery <SuperMarketDAL.Entitys.GoodsStock> .ExecuteOql(q, db); } } EntityQuery <GoodsSellDetail> queryDetail = new EntityQuery <GoodsSellDetail>(db); queryDetail.Insert(note.GoodsSellDetails); //更新会员的积分 if (integral > 0) { SuperMarketDAL.Entitys.CustomerContactInfo ccInfo = new CustomerContactInfo(); ccInfo.CustomerID = customer.CustomerID; ccInfo.Integral = integral; OQL qc = OQL.From(ccInfo) .UpdateSelf('+', ccInfo.Integral) .Where(ccInfo.CustomerID) .END; EntityQuery <SuperMarketDAL.Entitys.GoodsStock> .ExecuteOql(qc, db); } } db.Commit(); } catch (Exception ex) { db.Rollback(); throw new Exception("插入销售记录失败,内部错误原因:" + ex.Message); } return(note); }