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.

153 lines
4.7KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusDAL;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace CNAS_DBSync
  12. {
  13. public class SelectTableType
  14. {
  15. private static string connectionStr = "";
  16. public static DataTable MySqlsec(string strTableName)
  17. {
  18. MySQLDAL mysql = new MySQLDAL();
  19. DataTable tb = mysql.GetTableTypeAndLenth(strTableName);
  20. return tb;
  21. }
  22. public static DataTable Sqlserversec(string strTableName, SyncInstrumentItemInfo t)
  23. {
  24. DataTable dt = new DataTable();
  25. try
  26. {
  27. if (t.SyncInstrumentDSInfo.Host != "")
  28. connectionStr = $"Data Source = {t.SyncInstrumentDSInfo.Host}; Initial Catalog = {t.SyncInstrumentDSInfo.ServerName}; User Id = {t.SyncInstrumentDSInfo.UserId}; Password = {t.SyncInstrumentDSInfo.UserPwd};";
  29. string sql = string.Format(@"SELECT
  30. c.name AS ColumnName,
  31. t.name AS DataType,
  32. c.max_length AS MaxLength,
  33. c.is_nullable AS IsNullable,
  34. c.default_object_id AS DefaultObjectId,
  35. ep.value AS remark
  36. FROM
  37. sys.columns c
  38. JOIN
  39. sys.types t ON c.user_type_id = t.user_type_id
  40. LEFT JOIN
  41. sys.extended_properties ep
  42. ON
  43. c.object_id = ep.major_id
  44. AND c.column_id = ep.minor_id
  45. AND ep.name = 'MS_Description' -- 备注的属性名称
  46. WHERE
  47. c.object_id = OBJECT_ID('{0}'); ", strTableName); //查询字符串
  48. dt = GetDataTable(sql, new SqlParameter[] { });
  49. }
  50. catch (Exception ex)
  51. {
  52. //发生异常,写入日志
  53. AppLog.Error(ex.Message);
  54. }
  55. return dt;
  56. }
  57. /// <summary>
  58. /// 查询操作
  59. /// </summary>
  60. /// <param name="sql"></param>
  61. /// <returns></returns>
  62. public static DataTable GetDataTable(string sql, params SqlParameter[] sp)
  63. {
  64. using (SqlConnection conn = new SqlConnection(connectionStr))
  65. {
  66. conn.Open();
  67. using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
  68. {
  69. sda.SelectCommand.Parameters.AddRange(sp);
  70. DataTable dt = new DataTable();
  71. sda.Fill(dt);
  72. return dt;
  73. }
  74. }
  75. }
  76. public static DataTable PostgreSql(string strTableName)
  77. {
  78. DataTable dt = new DataTable();
  79. string strSql = string.Format(@"SELECT
  80. a.attname as ColumnName,
  81. format_type(a.atttypid, a.atttypmod) as DataType,
  82. a.attnotnull as 非空,
  83. col_description(a.attrelid, a.attnum) as remark
  84. FROM
  85. pg_class as c, pg_attribute as a
  86. where
  87. a.attrelid = c.oid
  88. and
  89. a.attnum > 0
  90. and
  91. c.relname = '{0}'; ", strTableName);
  92. try
  93. {
  94. dt = PostgreSQLHelper.ExecuteDataSet(strSql).Tables[0];
  95. }
  96. catch (Exception ex)
  97. {
  98. AppLog.Error(ex.Message);
  99. }
  100. return dt;
  101. }
  102. public static DataTable DmSql(string strTableName)
  103. {
  104. DataTable dt = new DataTable();
  105. string strSql = string.Format(@"SELECT
  106. a.COLUMN_NAME AS ColumnName,
  107. b.COMMENTS AS remark,
  108. a.DATA_TYPE || CASE
  109. WHEN a.DATA_TYPE IN('VARCHAR', 'CHAR') THEN '(' || a.DATA_LENGTH || ')'
  110. WHEN a.DATA_TYPE IN('DECIMAL', 'NUMERIC') THEN '(' || a.DATA_PRECISION || ',' || a.DATA_SCALE || ')'
  111. ELSE ''
  112. END AS DataType
  113. FROM
  114. ALL_TAB_COLUMNS a
  115. LEFT JOIN
  116. ALL_COL_COMMENTS b
  117. ON a.OWNER = b.OWNER
  118. AND a.TABLE_NAME = b.TABLE_NAME
  119. AND a.COLUMN_NAME = b.COLUMN_NAME
  120. WHERE
  121. a.TABLE_NAME = '{0}'
  122. ORDER BY
  123. a.COLUMN_ID;", strTableName);
  124. try
  125. {
  126. dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
  127. }
  128. catch (Exception ex)
  129. {
  130. AppLog.Error(ex.Message);
  131. }
  132. return dt;
  133. }
  134. }
  135. }