|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- using System;
- using System.Collections.Generic;
- using System.Data.OleDb;
- using System.Linq;
- using System.Text;
- using System.Data;
- using CnasSynchronousCommon;
-
- namespace CnasSynchronusDAL
- {
- public static class MdbDAL
- {
- /// <summary>
- /// 获取所有表名和表结构
- /// </summary>
- /// <param name="strPath"></param>
- /// <returns></returns>
- public static Dictionary<string, DataTable> ReadAccessTables(string strPath, string strPwd,string strAccessVersion)
- {
- Dictionary<string, DataTable> dictTables = new Dictionary<string, DataTable>();
- try
- {
- //连接字符串
- string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- if(!string.IsNullOrWhiteSpace(strAccessVersion)&&strAccessVersion != "0")
- connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- 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;
- 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]);
- }
- }
- }
- catch (Exception ex)
- {
- //发生异常,写入日志
- AppLog.Error(ex.Message);
- }
- return dictTables;
- }
-
- internal static DataTable ReadAccessTableStruct(string strPath, string strPwd, string strAccessVersion, string strViewName, string strViewSQL,string strTableName)
- {
- DataTable dt = new DataTable();
- try
- {
- //连接字符串
- string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- if (!string.IsNullOrWhiteSpace(strAccessVersion) && strAccessVersion != "0")
- connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- 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;
- if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
- {
- if(strViewSQL.ToUpper().Contains("WHERE"))
- sql = strViewSQL + " and 1=0";
- else
- sql = strViewSQL + " where 1=0";
- }
- 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];
- }
- }
- catch (Exception ex)
- {
- //发生异常,写入日志
- AppLog.Error(ex.Message);
- }
- return dt;
- }
-
- 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();
- try
- {
- AppLog.Info("开始执行");
- //连接字符串
- string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- if (!string.IsNullOrWhiteSpace(strAccessVersion) && strAccessVersion != "0")
- connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- //执行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))
- {
- conn.Open();
-
- //获取数据库表结构
- DataTable dtStruct = new DataTable();
- 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())
- {
- bIfSuccess = true;
- DataRow row = dtStruct.NewRow();
- //填充一行数据
- for (int i = 0; i < dtStruct.Columns.Count; i++)
- {
-
- string columnName = dtStruct.Columns[i].ColumnName;
-
- //判断是否能够正常读取单元格数据
- object objValue = null;
- 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))
- {
- if (dtTime > Convert.ToDateTime(strDate))
- {
- row[i] = objValue;
- }
- else
- {
- bIfSuccess = false;
- }
- }
- 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;
- }
-
- //如果有一个列没有成功,后续的列不需要再插入,直接退出循环
- if (!bIfSuccess)
- break;
- }
-
- //添加一行数据
- if (bIfSuccess)
- {
- dtStruct.Rows.Add(row);
- }
- row = null;
- }
- reader.Close();
-
- AppLog.Info("ReadAccessTablesByDate执行的参数:" + strDate+","+ sql);
- AppLog.Info("ReadAccessTablesByDate读取到的数据行数:" + dtStruct.Rows.Count);
-
- /*将数据库中某些特殊类型的日期数据转换一下格式,便于后续处理*/
- dt = DateAndTimeTypeOpera(dtStruct, strDateColumn,strSpecialDateFormat);
-
- AppLog.Info("结束执行");
- }
- }
- catch (Exception ex)
- {
- //发生异常,写入日志
- AppLog.Error(ex.Message);
- }
- return dt;
- }
-
- private static bool IfSepcialDateFormat(string strValue,string strSpecialDateFormat, ref DateTime dtTime)
- {
- bool bIfTrue = true;
- try
- {
- dtTime = DateTime.ParseExact(strValue, strSpecialDateFormat, new System.Globalization.CultureInfo("zh-CN", true));
- }
- catch (Exception e)
- {
- bIfTrue = false;
- AppLog.Error(e.Message);
- }
- return bIfTrue;
- }
-
- public static DataTable ReadAccessTablesByTableName(string strPath, string strUser, string strPwd, string strTableName,string strAccessVersion)
- {
- DataSet set = new DataSet();
- try
- {
- //连接字符串
- string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- if (!string.IsNullOrWhiteSpace(strAccessVersion) && strAccessVersion != "0")
- connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
- 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);
- }
- }
- catch (Exception ex)
- {
- //发生异常,写入日志
- AppLog.Error(ex.Message);
- }
- return set.Tables[0];
- }
-
-
- /// <summary>
- /// 获取数据时,单独处理某些(Date和Time)类型数据,并把数据类型转换为字符串类型
- /// </summary>
- private static DataTable DateAndTimeTypeOpera(DataTable dt,string strDateColumn,string strSpecialDateFormat)
- {
- DataTable dtNewFormat = new DataTable();
- List<string> lstChangeColumn = new List<string>();
- //添加列
- foreach (DataColumn dc in dt.Columns)
- {
- string strDateType = dc.DataType.ToString();
- switch (strDateType.ToUpper())
- {
- case "SYSTEM.DATETIME":
- dtNewFormat.Columns.Add(dc.ColumnName, typeof(string)); //使用字符串来存储该字段,而不是采用它的数据库格式(C#无法区分Date, Time,DateTime,前两种格式会自动补充数据,导致数据的不准确)
- lstChangeColumn.Add(dc.ColumnName);
- break;
- default:
- dtNewFormat.Columns.Add(dc.ColumnName, dc.DataType);
- break;
- }
- }
-
- //添加数据行
- foreach (DataRow dr in dt.Rows)
- {
- DataRow drNewRow = dtNewFormat.NewRow();
- foreach (DataColumn dc in dtNewFormat.Columns)
- {
- if (lstChangeColumn.Contains(dc.ColumnName))
- {
- if (dr[dc.ColumnName] != null && dr[dc.ColumnName].ToString() != "")
- {
- DateTime dateTime = Convert.ToDateTime(dr[dc.ColumnName]);
- string strTime = dateTime.ToString("HH:mm:ss");
- if (dateTime <= Convert.ToDateTime("1900-01-02"))
- {
- drNewRow[dc.ColumnName] = strTime;
- }
- else
- {
- if (strTime == "00:00:00")
- drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd");
- else
- drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
- }
- else
- {
- if (dc.ColumnName.ToLower() == strDateColumn.ToLower())
- {
- DateTime dt1 = DateTime.Now;
- if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt1))
- {
- drNewRow[dc.ColumnName] = dr[dc.ColumnName].ToString();
- }
- else
- {
- DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), strSpecialDateFormat, new System.Globalization.CultureInfo("zh-CN", true));
- if (dtTime.ToString("HH:mm:ss") == "00:00:00")
- drNewRow[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
- else
- drNewRow[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
- else
- {
- drNewRow[dc.ColumnName] = dr[dc.ColumnName];
- }
- }
- }
- dtNewFormat.Rows.Add(drNewRow);
- }
-
- //返回数据
- return dtNewFormat;
- }
-
- }
- }
|