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.

BaseDAL.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. namespace CNAS_DBSync
  8. {
  9. public class BaseDAL
  10. {
  11. private static log4net.ILog log = log4net.LogManager.GetLogger("BaseDAL");
  12. public void CreateConnectString(string strHost, string strName, string strUser, string strPwd)
  13. {
  14. SQLDB2Helper.createConnectString(strHost, strName, strUser, strPwd);
  15. }
  16. //获取所有表单名称
  17. public DataTable GetTableNames(string strUser)
  18. {
  19. DataTable dt = new DataTable();
  20. string strSql = string.Format("SELECT TABNAME FROM SYSCAT.TABLES Where TABSCHEMA = '{0}'",strUser);
  21. try
  22. {
  23. dt= SQLDB2Helper.getDB2Table(strSql);
  24. }
  25. catch (Exception ex)
  26. {
  27. log.Error(ex.Message);
  28. }
  29. return dt;
  30. }
  31. /// <summary>
  32. /// 获取某表的表结构
  33. /// </summary>
  34. /// <param name="strTableName"></param>
  35. /// <returns></returns>
  36. public DataTable GetTableStruct(string strTableName)
  37. {
  38. DataTable dt = new DataTable();
  39. string strSql = string.Format("SELECT * FROM {0} Where 1=0", strTableName);
  40. try
  41. {
  42. dt = SQLDB2Helper.getDB2Table(strSql);
  43. }
  44. catch (Exception ex)
  45. {
  46. log.Error(ex.Message);
  47. }
  48. return dt;
  49. }
  50. /// <summary>
  51. /// 逐行批量插入数据
  52. /// </summary>
  53. /// <param name="dt"></param>
  54. /// <returns></returns>
  55. public int InsertCnasData(DataTable dt, List<SyncParamasInfo> syncParamasInfos)
  56. {
  57. int iReturn = 0;
  58. if (dt.Rows.Count <= 0) return 0;
  59. try
  60. {
  61. //构建SQL语句
  62. string strSql_part1 = "";
  63. string strSql_part2 = "";
  64. foreach (var item in syncParamasInfos)
  65. {
  66. strSql_part1 += item.TargetField + ",";
  67. strSql_part2 += "?,";
  68. }
  69. string strSql = string.Format("insert into {0}({1}) values({2})",syncParamasInfos[0].TargetTable, strSql_part1.Substring(0, strSql_part1.Length - 1), strSql_part2.Substring(0, strSql_part2.Length - 1));
  70. foreach (DataRow dr in dt.Rows)
  71. {
  72. OleDbParameter[] parameters = new OleDbParameter[syncParamasInfos.Count];
  73. for (int i = 0; i < syncParamasInfos.Count; i++)
  74. {
  75. parameters[i] = new OleDbParameter(syncParamasInfos[i].TargetField, dr[syncParamasInfos[i].TargetField]);
  76. }
  77. //执行SQL语句
  78. iReturn += SQLDB2Helper.ExequeryDB2Table(strSql, parameters);
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. //此处添加错误日志
  84. log.Error(ex.Message);
  85. }
  86. return iReturn;
  87. }
  88. public bool LinkCnasTest()
  89. {
  90. return SQLDB2Helper.TestCnasLink();
  91. }
  92. }
  93. }