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.

SelectTableType.cs 4.5KB

4 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. c.COLUMN_NAME AS ColumnName,
  107. c.DATA_TYPE AS DataType,
  108. c.DATA_LENGTH AS 字段长度,
  109. c.NULLABLE AS 是否允许为空,
  110. com.COMMENTS AS remark
  111. FROM
  112. USER_TAB_COLUMNS c
  113. LEFT JOIN
  114. USER_COL_COMMENTS com
  115. ON
  116. c.TABLE_NAME = com.TABLE_NAME
  117. AND c.COLUMN_NAME = com.COLUMN_NAME
  118. WHERE
  119. c.TABLE_NAME = '{0}';", strTableName);
  120. try
  121. {
  122. dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
  123. }
  124. catch (Exception ex)
  125. {
  126. AppLog.Error(ex.Message);
  127. }
  128. return dt;
  129. }
  130. }
  131. }