CNAS取数仪器端升级
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

300 linhas
14KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.OleDb;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Data;
  7. using CnasSynchronousCommon;
  8. using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
  9. using Newtonsoft.Json.Linq;
  10. using Dm;
  11. namespace CnasSynchronusDAL
  12. {
  13. public static class MdbDAL
  14. {
  15. /// <summary>
  16. /// 获取所有表名和表结构
  17. /// </summary>
  18. /// <param name="strPath"></param>
  19. /// <returns></returns>
  20. public static Dictionary<string, DataTable> ReadAccessTables(string strPath, string strPwd,string strAccessVersion)
  21. {
  22. Dictionary<string, DataTable> dictTables = new Dictionary<string, DataTable>();
  23. try
  24. {
  25. //连接字符串
  26. string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  27. if(!string.IsNullOrWhiteSpace(strAccessVersion)&&strAccessVersion != "0")
  28. connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  29. using (OleDbConnection conn = new OleDbConnection(connstring))
  30. {
  31. conn.Open();
  32. DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  33. foreach (DataRow dr in TablesName.Rows)
  34. {
  35. string strTableName = dr[2].ToString();
  36. string sql = string.Format("SELECT * FROM [{0}] where 1=0", strTableName); //查询字符串
  37. using (OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring))
  38. {
  39. DataSet set = new DataSet();
  40. ada.Fill(set);
  41. dictTables.Add(strTableName.ToUpper(), set.Tables[0]);
  42. }
  43. }
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. //发生异常,写入日志
  49. AppLog.Error(ex.Message);
  50. }
  51. return dictTables;
  52. }
  53. internal static DataTable ReadAccessTableStruct(string strPath, string strPwd, string strAccessVersion, string strViewName, string strViewSQL,string strTableName)
  54. {
  55. DataTable dt = new DataTable();
  56. try
  57. {
  58. //连接字符串
  59. string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  60. if (!string.IsNullOrWhiteSpace(strAccessVersion) && strAccessVersion != "0")
  61. connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  62. using (OleDbConnection conn = new OleDbConnection(connstring))
  63. {
  64. conn.Open();
  65. //DataTable TablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  66. string sql = "";
  67. if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
  68. {
  69. if(strViewSQL.ToUpper().Contains("WHERE"))
  70. sql = strViewSQL + " and 1=0";
  71. else
  72. sql = strViewSQL + " where 1=0";
  73. }
  74. else
  75. sql = string.Format("select * from [{0}] where 1=0", strTableName); //查询字符串
  76. using (OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring))
  77. {
  78. DataSet set = new DataSet();
  79. ada.Fill(set);
  80. dt = set.Tables[0];
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. //发生异常,写入日志
  87. AppLog.Error(ex.Message);
  88. }
  89. return dt;
  90. }
  91. public static DataTable ReadAccessTablesByDate(string strPath, string strPwd, string strTableName, string strDateColumn, string strDate, string strAccessVersion,string strSpecialDateFormat,string strViewName, string strViewSQL)
  92. {
  93. DataTable dt = new DataTable();
  94. OleDbConnection conn = null;
  95. try
  96. {
  97. AppLog.Info("开始执行");
  98. //连接字符串
  99. string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  100. if (!string.IsNullOrWhiteSpace(strAccessVersion) && strAccessVersion != "0")
  101. connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  102. //执行sql语句
  103. string sql = "";
  104. if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
  105. {
  106. sql = strViewSQL;
  107. }
  108. else
  109. {
  110. sql = $"select * from [{strTableName}]" + "\r\n" +
  111. $"where {strDateColumn} is not null and ? < {strDateColumn}";
  112. }
  113. using (conn = new OleDbConnection(connstring))
  114. {
  115. conn.Open();
  116. //获取数据库表结构
  117. DataTable dtStruct = new DataTable();
  118. dtStruct = ReadAccessTableStruct(strPath, strPwd, strAccessVersion, strViewName, strViewSQL, strTableName);
  119. //获取数据,并插入到表中
  120. using (OleDbCommand command = new OleDbCommand(sql, conn))
  121. {
  122. if (strViewName != strTableName || string.IsNullOrWhiteSpace(strViewName))
  123. {
  124. string datetime = string.IsNullOrWhiteSpace(strSpecialDateFormat) ? strDate : Convert.ToDateTime(strDate).ToString(strSpecialDateFormat);
  125. command.Parameters.AddWithValue("?", datetime);
  126. }
  127. using (OleDbDataReader reader = command.ExecuteReader())
  128. {
  129. try
  130. {
  131. while (reader.Read())
  132. {
  133. DataRow row = dtStruct.NewRow();
  134. for (int i = 0; i < dtStruct.Columns.Count; i++)
  135. {
  136. row[i] = reader.GetValue(i);
  137. }
  138. dtStruct.Rows.Add(row);
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. //发生异常,写入日志
  144. AppLog.Error(ex.Message);
  145. }
  146. }
  147. }
  148. AppLog.Info("ReadAccessTablesByDate执行的参数:" + strDate+","+ sql);
  149. AppLog.Info("ReadAccessTablesByDate读取到的数据行数:" + dtStruct.Rows.Count);
  150. /*将数据库中某些特殊类型的日期数据转换一下格式,便于后续处理*/
  151. dt = DateAndTimeTypeOpera(dtStruct, strDateColumn,strSpecialDateFormat);
  152. AppLog.Info("结束执行");
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. //发生异常,写入日志
  158. AppLog.Error(ex.Message);
  159. }
  160. return dt;
  161. }
  162. private static bool IfSepcialDateFormat(string strValue,string strSpecialDateFormat, ref DateTime dtTime)
  163. {
  164. bool bIfTrue = true;
  165. try
  166. {
  167. dtTime = DateTime.ParseExact(strValue, strSpecialDateFormat, new System.Globalization.CultureInfo("zh-CN", true));
  168. }
  169. catch (Exception e)
  170. {
  171. bIfTrue = false;
  172. AppLog.Error(e.Message);
  173. }
  174. return bIfTrue;
  175. }
  176. public static DataTable ReadAccessTablesByTableName(string strPath, string strUser, string strPwd, string strTableName,string strAccessVersion)
  177. {
  178. DataSet set = new DataSet();
  179. try
  180. {
  181. //连接字符串
  182. string connstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  183. if (!string.IsNullOrWhiteSpace(strAccessVersion) && strAccessVersion != "0")
  184. connstring = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", strPath, strPwd);
  185. using (OleDbConnection conn = new OleDbConnection(connstring))
  186. {
  187. conn.Open();
  188. string sql = string.Format("SELECT * FROM [{0}]", strTableName); //查询字符串
  189. using (OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring))
  190. {
  191. ada.Fill(set);
  192. }
  193. }
  194. }
  195. catch (Exception ex)
  196. {
  197. //发生异常,写入日志
  198. AppLog.Error(ex.Message);
  199. }
  200. return set.Tables[0];
  201. }
  202. /// <summary>
  203. /// 获取数据时,单独处理某些(Date和Time)类型数据,并把数据类型转换为字符串类型
  204. /// </summary>
  205. private static DataTable DateAndTimeTypeOpera(DataTable dt,string strDateColumn,string strSpecialDateFormat)
  206. {
  207. DataTable dtNewFormat = new DataTable();
  208. List<string> lstChangeColumn = new List<string>();
  209. //添加列
  210. foreach (DataColumn dc in dt.Columns)
  211. {
  212. string strDateType = dc.DataType.ToString();
  213. switch (strDateType.ToUpper())
  214. {
  215. case "SYSTEM.DATETIME":
  216. dtNewFormat.Columns.Add(dc.ColumnName, typeof(string)); //使用字符串来存储该字段,而不是采用它的数据库格式(C#无法区分Date, Time,DateTime,前两种格式会自动补充数据,导致数据的不准确)
  217. lstChangeColumn.Add(dc.ColumnName);
  218. break;
  219. default:
  220. dtNewFormat.Columns.Add(dc.ColumnName, dc.DataType);
  221. break;
  222. }
  223. }
  224. //添加数据行
  225. foreach (DataRow dr in dt.Rows)
  226. {
  227. DataRow drNewRow = dtNewFormat.NewRow();
  228. foreach (DataColumn dc in dtNewFormat.Columns)
  229. {
  230. if (lstChangeColumn.Contains(dc.ColumnName))
  231. {
  232. if (dr[dc.ColumnName] != null && dr[dc.ColumnName].ToString() != "")
  233. {
  234. DateTime dateTime = Convert.ToDateTime(dr[dc.ColumnName]);
  235. string strTime = dateTime.ToString("HH:mm:ss");
  236. if (dateTime <= Convert.ToDateTime("1900-01-02"))
  237. {
  238. drNewRow[dc.ColumnName] = strTime;
  239. }
  240. else
  241. {
  242. if (strTime == "00:00:00")
  243. drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd");
  244. else
  245. drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd HH:mm:ss");
  246. }
  247. }
  248. }
  249. else
  250. {
  251. if (dc.ColumnName.ToLower() == strDateColumn.ToLower())
  252. {
  253. DateTime dt1 = DateTime.Now;
  254. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt1))
  255. {
  256. drNewRow[dc.ColumnName] = dr[dc.ColumnName].ToString();
  257. }
  258. else
  259. {
  260. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), strSpecialDateFormat, new System.Globalization.CultureInfo("zh-CN", true));
  261. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  262. drNewRow[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  263. else
  264. drNewRow[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  265. }
  266. }
  267. else
  268. {
  269. drNewRow[dc.ColumnName] = dr[dc.ColumnName];
  270. }
  271. }
  272. }
  273. dtNewFormat.Rows.Add(drNewRow);
  274. }
  275. //返回数据
  276. return dtNewFormat;
  277. }
  278. }
  279. }