CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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