/// <summary> /// 通过事务执行多条SQL语句 /// </summary> /// <param name="sqlList">SQL语句集</param> /// <returns></returns> public static DataResult ExecuteByTransaction(List <string> sqlList) { DataResult result = new DataResult(); using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbComm.Connection = IDbConn; IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted);// DataBaseFactory.GetIDbTransaction(); if (IDbTrans != null) { IDbComm.Transaction = IDbTrans; // IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); } try { for (int i = 0; i < sqlList.Count; i++) { string str = sqlList[i].ToString(); if (str.Trim().Length > 1) { IDbComm.CommandText = str; result.RecordCount += IDbComm.ExecuteNonQuery(); } } IDbTrans.Commit(); return(result); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); result.Error = exception.Message; return(result); } finally { IDbConn.Close(); } } }
/// <summary> /// 通过事务执行多条SQL语句 /// </summary> /// <param name="sqlList">SQL语句集</param> /// <returns></returns> public bool ExecuteByTransaction(List <string> sqlList) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbComm.Connection = IDbConn; IDbTrans = IDbConn.BeginTransaction();// DataBaseFactory.GetIDbTransaction(); if (IDbTrans != null) { IDbComm.Transaction = IDbTrans; IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); } try { for (int i = 0; i < sqlList.Count; i++) { string str = sqlList[i].ToString(); if (str.Trim().Length > 1) { IDbComm.CommandText = str; IDbComm.ExecuteNonQuery(); } } IDbTrans.Commit(); return(true); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); return(false); throw new Exception(exception.Message); } finally { IDbConn.Close(); } } }
/// <summary> /// 提交事务 /// </summary> public static DataResult CommitTransaction() { DataResult result = new DataResult(); try { IDbTrans.Commit(); return(result); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); result.Error = exception.Message; return(result); } finally { IDbConn.Close(); } }
protected virtual void Each_FileHandleEvent(FileInfo info) { //判断文件是否符合要求 bool meet = true; meet = !info.Name.Contains("~$") && (EvFile?.IsMeet(info.Name) ?? false); if (meet) { //生成MD5 string md5 = ToGenerateMD5(info); MF file = new MF() { FID = md5, FileName = info.Name, Date = Convert.ToDateTime(EvFile.Extract(info.Name)) }; //查询数据库,判断是否已经读取过 bool readed = false; if (JoinDb) { using (IDbQuery query = DbIns.GetQuery("Cq")) { if (query != null) { query.Open(); object obj = query.SelectToValue(new QueryArgs() { Where = "FID=@FID", Rows = "Count(1)", Param = new DbParams[] { new DbParams("FID", md5) }, Group = "FID" }); int count = Convert.ToInt32(obj); readed = count > 0; } } } //循环读取 if (!readed) { using (IExcReader reader = ExcIns.GetReader()) { bool isopen = reader?.Open(info.FullName) ?? false; if (CellHelper != null && isopen) { CellHelper.ToObtainColumn = new string[] { "Target", "Sample", "Cq" }; using (IDbAlter alter = DbIns.GetAlter("Cq")) { alter.Open(); IDbTrans trans = DbIns.GetTrans(); trans.Begin(); while (reader.Read()) { Record cq = null; //如果列索引为空则查找索引 if (!CellHelper.IsFindColumn) { CellHelper.FindColumnIndex(reader.Columns); } //不为空则将单元格转化为 Cq ; else { CellArgs args = CellHelper.GetArgs(reader); args.File = file; cq = CqHelper.ConvertToCq(args); } //将 Cq 插入到分类集合中 if (cq != null) { if (JoinDb) { try { alter.Insert(cq, "ID"); } catch { trans.Rollback(); } } CqHelper?.InsertToCollection(cq, Group); } } trans.Commit(); } } } } } }