diff --git a/CnasSynchronusClient/InsturmentData/ExcelInstrumentData.cs b/CnasSynchronusClient/InsturmentData/ExcelInstrumentData.cs index 9847678..129636e 100644 --- a/CnasSynchronusClient/InsturmentData/ExcelInstrumentData.cs +++ b/CnasSynchronusClient/InsturmentData/ExcelInstrumentData.cs @@ -28,7 +28,7 @@ namespace CnasSynchronusClient /// /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径 /// - public List LstFileFullName { get; set; } + public List LstFileFullName { get; set; } = new List(); public ExcelInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs) { diff --git a/CnasSynchronusDAL/DAL/MdbDAL.cs b/CnasSynchronusDAL/DAL/MdbDAL.cs index b06574f..b62107f 100644 --- a/CnasSynchronusDAL/DAL/MdbDAL.cs +++ b/CnasSynchronusDAL/DAL/MdbDAL.cs @@ -5,6 +5,9 @@ using System.Linq; using System.Text; using System.Data; using CnasSynchronousCommon; +using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; +using Newtonsoft.Json.Linq; +using Dm; namespace CnasSynchronusDAL { @@ -29,18 +32,16 @@ namespace CnasSynchronusDAL conn.Open(); DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字 - string sql = ""; - OleDbDataAdapter ada = null; foreach (DataRow dr in TablesName.Rows) { string strTableName = dr[2].ToString(); - - sql = string.Format("SELECT * FROM [{0}] where 1=0", strTableName); //查询字符串 - ada = new OleDbDataAdapter(sql, connstring); - DataSet set = new DataSet(); - ada.Fill(set); - - dictTables.Add(strTableName.ToUpper(), set.Tables[0]); + string sql = string.Format("SELECT * FROM [{0}] where 1=0", strTableName); //查询字符串 + using (OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring)) + { + DataSet set = new DataSet(); + ada.Fill(set); + dictTables.Add(strTableName.ToUpper(), set.Tables[0]); + } } } } @@ -67,7 +68,6 @@ namespace CnasSynchronusDAL //DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字 string sql = ""; - OleDbDataAdapter ada = null; if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName)) { if(strViewSQL.ToUpper().Contains("WHERE")) @@ -77,11 +77,13 @@ namespace CnasSynchronusDAL } else sql = string.Format("select * from [{0}] where 1=0", strTableName); //查询字符串 - ada = new OleDbDataAdapter(sql, connstring); - DataSet set = new DataSet(); - ada.Fill(set); - dt = set.Tables[0]; + using (OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring)) + { + DataSet set = new DataSet(); + ada.Fill(set); + dt = set.Tables[0]; + } } } catch (Exception ex) @@ -95,6 +97,7 @@ namespace CnasSynchronusDAL public static DataTable ReadAccessTablesByDate(string strPath, string strPwd, string strTableName, string strDateColumn, string strDate, string strAccessVersion,string strSpecialDateFormat,string strViewName, string strViewSQL) { DataTable dt = new DataTable(); + OleDbConnection conn = null; try { AppLog.Info("开始执行"); @@ -105,91 +108,53 @@ namespace CnasSynchronusDAL //执行sql语句 string sql = ""; if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName)) + { sql = strViewSQL; + } else - sql = string.Format("select * from [{0}] where {1} is not null", strTableName, strDateColumn); //查询字符串 - - using (OleDbConnection conn = new OleDbConnection(connstring)) + { + sql = $"select * from [{strTableName}]" + "\r\n" + + $"where {strDateColumn} is not null and ? < {strDateColumn}"; + } + + using (conn = new OleDbConnection(connstring)) { conn.Open(); //获取数据库表结构 DataTable dtStruct = new DataTable(); - dtStruct=ReadAccessTableStruct(strPath, strPwd, strAccessVersion, strViewName, strViewSQL, strTableName); + dtStruct = ReadAccessTableStruct(strPath, strPwd, strAccessVersion, strViewName, strViewSQL, strTableName); //获取数据,并插入到表中 - OleDbCommand command = new OleDbCommand(sql, conn); - OleDbDataReader reader = command.ExecuteReader(); - DateTime dtTime = DateTime.Now; - bool bIfSuccess = true; - while (reader.Read()) + using (OleDbCommand command = new OleDbCommand(sql, conn)) { - bIfSuccess = true; - DataRow row = dtStruct.NewRow(); - //填充一行数据 - for (int i = 0; i < dtStruct.Columns.Count; i++) + if (strViewName != strTableName || string.IsNullOrWhiteSpace(strViewName)) { + string datetime = string.IsNullOrWhiteSpace(strSpecialDateFormat) ? strDate : Convert.ToDateTime(strDate).ToString(strSpecialDateFormat); + command.Parameters.AddWithValue("?", datetime); + } - string columnName = dtStruct.Columns[i].ColumnName; - - //判断是否能够正常读取单元格数据 - object objValue = null; + using (OleDbDataReader reader = command.ExecuteReader()) + { try { - objValue = reader.GetValue(i); - } - catch (Exception ee) - { - AppLog.Error("读取数据行时发生错误,无法正常读取,错误提示:"+ee.Message); - bIfSuccess = false; - break; - } - - if (columnName.ToLower() == strDateColumn.ToLower()) //处理关键日期字段 --将日期字段转换成可识别的数据,然后比较日期大小 - { - if (DateTime.TryParse(objValue.ToString(), out dtTime)) + while (reader.Read()) { - if (dtTime > Convert.ToDateTime(strDate)) - { - row[i] = objValue; - } - else + DataRow row = dtStruct.NewRow(); + for (int i = 0; i < dtStruct.Columns.Count; i++) { - bIfSuccess = false; + row[i] = reader.GetValue(i); } + dtStruct.Rows.Add(row); } - else if (IfSepcialDateFormat(objValue.ToString(), strSpecialDateFormat, ref dtTime)) - { - if (dtTime > Convert.ToDateTime(strDate)) - { - row[i] = objValue; - } - else - { - bIfSuccess = false; - } - } - else - bIfSuccess = false; } - else //处理标准字段 - { - row[i] = objValue; + catch (Exception ex) + { + //发生异常,写入日志 + AppLog.Error(ex.Message); } - - //如果有一个列没有成功,后续的列不需要再插入,直接退出循环 - if (!bIfSuccess) - break; - } - - //添加一行数据 - if (bIfSuccess) - { - dtStruct.Rows.Add(row); } - row = null; } - reader.Close(); AppLog.Info("ReadAccessTablesByDate执行的参数:" + strDate+","+ sql); AppLog.Info("ReadAccessTablesByDate读取到的数据行数:" + dtStruct.Rows.Count); @@ -235,14 +200,11 @@ namespace CnasSynchronusDAL using (OleDbConnection conn = new OleDbConnection(connstring)) { conn.Open(); - DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字 - - string sql = ""; - OleDbDataAdapter ada = null; - sql = string.Format("SELECT * FROM [{0}]", strTableName); //查询字符串 - ada = new OleDbDataAdapter(sql, connstring); - - ada.Fill(set); + string sql = string.Format("SELECT * FROM [{0}]", strTableName); //查询字符串 + using (OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring)) + { + ada.Fill(set); + } } } catch (Exception ex)