Browse Source

优化大Access的读取。

develop
曾凯 4 months ago
parent
commit
2f7cf5abb5
2 changed files with 48 additions and 86 deletions
  1. +1
    -1
      CnasSynchronusClient/InsturmentData/ExcelInstrumentData.cs
  2. +47
    -85
      CnasSynchronusDAL/DAL/MdbDAL.cs

+ 1
- 1
CnasSynchronusClient/InsturmentData/ExcelInstrumentData.cs View File

@@ -28,7 +28,7 @@ namespace CnasSynchronusClient
/// <summary> /// <summary>
/// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径 /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径
/// </summary> /// </summary>
public List<string> LstFileFullName { get; set; }
public List<string> LstFileFullName { get; set; } = new List<string>();


public ExcelInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs) public ExcelInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs)
{ {


+ 47
- 85
CnasSynchronusDAL/DAL/MdbDAL.cs View File

@@ -5,6 +5,9 @@ using System.Linq;
using System.Text; using System.Text;
using System.Data; using System.Data;
using CnasSynchronousCommon; using CnasSynchronousCommon;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using Newtonsoft.Json.Linq;
using Dm;


namespace CnasSynchronusDAL namespace CnasSynchronusDAL
{ {
@@ -29,18 +32,16 @@ namespace CnasSynchronusDAL
conn.Open(); conn.Open();
DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字 DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字


string sql = "";
OleDbDataAdapter ada = null;
foreach (DataRow dr in TablesName.Rows) foreach (DataRow dr in TablesName.Rows)
{ {
string strTableName = dr[2].ToString(); 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的名字 //DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
string sql = ""; string sql = "";
OleDbDataAdapter ada = null;
if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName)) if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
{ {
if(strViewSQL.ToUpper().Contains("WHERE")) if(strViewSQL.ToUpper().Contains("WHERE"))
@@ -77,11 +77,13 @@ namespace CnasSynchronusDAL
} }
else else
sql = string.Format("select * from [{0}] where 1=0", strTableName); //查询字符串 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) 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) 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(); DataTable dt = new DataTable();
OleDbConnection conn = null;
try try
{ {
AppLog.Info("开始执行"); AppLog.Info("开始执行");
@@ -105,91 +108,53 @@ namespace CnasSynchronusDAL
//执行sql语句 //执行sql语句
string sql = ""; string sql = "";
if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName)) if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
{
sql = strViewSQL; sql = strViewSQL;
}
else 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(); conn.Open();
//获取数据库表结构 //获取数据库表结构
DataTable dtStruct = new DataTable(); 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 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执行的参数:" + strDate+","+ sql);
AppLog.Info("ReadAccessTablesByDate读取到的数据行数:" + dtStruct.Rows.Count); AppLog.Info("ReadAccessTablesByDate读取到的数据行数:" + dtStruct.Rows.Count);
@@ -235,14 +200,11 @@ namespace CnasSynchronusDAL
using (OleDbConnection conn = new OleDbConnection(connstring)) using (OleDbConnection conn = new OleDbConnection(connstring))
{ {
conn.Open(); 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) catch (Exception ex)


Loading…
Cancel
Save