CNAS取数仪器端升级
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

127 lignes
4.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using MySql.Data.MySqlClient;
  7. namespace CNAS_DBSync
  8. {
  9. public class MySQLBaseDAL
  10. {
  11. private static log4net.ILog log = log4net.LogManager.GetLogger("MySQLBaseDAL");
  12. public void CreateConnectString(string strIP,string strPort,string strName, string strUser, string strPwd)
  13. {
  14. MySQLHelper.InitConnectionString(strIP,strPort,strName,strUser,strPwd);
  15. }
  16. //获取所有表单名称
  17. public DataTable GetTableNames(string strName)
  18. {
  19. DataTable dt = new DataTable();
  20. string strSql = string.Format("SELECT table_name as TABNAME FROM information_schema.TABLES WHERE table_schema='{0}'", strName);
  21. try
  22. {
  23. dt = MySQLHelper.ExecuteDataSet(strSql).Tables[0];
  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 = MySQLHelper.ExecuteDataSet(strSql).Tables[0];
  43. }
  44. catch (Exception ex)
  45. {
  46. log.Error(ex.Message);
  47. }
  48. return dt;
  49. }
  50. /// <summary>
  51. /// 获取某表的表结构和类型长度
  52. /// </summary>
  53. /// <param name="strTableName"></param>
  54. /// <returns></returns>
  55. public DataTable GetTableTypeAndLenth(string strTableName)
  56. {
  57. DataTable dt = new DataTable();
  58. string strSql = string.Format("select COLUMN_NAME AS 'ColumnName',IS_NULLABLE AS 'IsNullable',DATA_TYPE AS 'DataType',CHARACTER_MAXIMUM_LENGTH AS 'CharMaxLenth',CHARACTER_OCTET_LENGTH AS 'CharOcterLenth',NUMERIC_PRECISION AS 'NumericPrecision',NUMERIC_SCALE AS 'NumericScale' FROM information_schema.COLUMNS WHERE TABLE_NAME LIKE '{0}'", strTableName);
  59. try
  60. {
  61. dt = MySQLHelper.ExecuteDataSet(strSql).Tables[0];
  62. }
  63. catch (Exception ex)
  64. {
  65. log.Error(ex.Message);
  66. }
  67. return dt;
  68. }
  69. /// <summary>
  70. /// 逐行批量插入数据
  71. /// </summary>
  72. /// <param name="dt"></param>
  73. /// <returns></returns>
  74. public int InsertCnasData(DataTable dt, List<SyncParamasInfo> syncParamasInfos)
  75. {
  76. int iReturn = 0;
  77. if (dt.Rows.Count <= 0) return 0;
  78. try
  79. {
  80. //构建SQL语句
  81. string strSql_part1 = "";
  82. string strSql_part2 = "";
  83. foreach (var item in syncParamasInfos)
  84. {
  85. strSql_part1 += item.TargetField + ",";
  86. strSql_part2 += string.Format("@{0},",item.TargetField);
  87. }
  88. 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));
  89. foreach (DataRow dr in dt.Rows)
  90. {
  91. MySqlParameter[] parameters = new MySqlParameter[syncParamasInfos.Count];
  92. for (int i = 0; i < syncParamasInfos.Count; i++)
  93. {
  94. parameters[i] = new MySqlParameter(syncParamasInfos[i].TargetField, dr[syncParamasInfos[i].TargetField]);
  95. }
  96. //执行SQL语句
  97. iReturn += MySQLHelper.ExecuteNonQuery(strSql, parameters);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. //此处添加错误日志
  103. log.Error(ex.Message);
  104. }
  105. return iReturn;
  106. }
  107. public bool LinkCnasTest()
  108. {
  109. return MySQLHelper.TestConnectMySql();
  110. }
  111. }
  112. }