CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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