CNAS取数仪器端升级
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

192 行
6.5KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusDAL;
  3. using CnasSynchrousModel;
  4. using Oracle.ManagedDataAccess.Client;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace CNAS_DBSync
  13. {
  14. public class SelectTableType
  15. {
  16. private static string connectionStr = "";
  17. public static DataTable MySqlsec(string strTableName)
  18. {
  19. MySQLDAL mysql = new MySQLDAL();
  20. DataTable tb = mysql.GetTableTypeAndLenth(strTableName);
  21. return tb;
  22. }
  23. public static DataTable Sqlserversec(string strTableName, SyncInstrumentItemInfo t)
  24. {
  25. DataTable dt = new DataTable();
  26. try
  27. {
  28. if (t.SyncInstrumentDSInfo.Host != "")
  29. connectionStr = $"Data Source = {t.SyncInstrumentDSInfo.Host}; Initial Catalog = {t.SyncInstrumentDSInfo.ServerName}; User Id = {t.SyncInstrumentDSInfo.UserId}; Password = {t.SyncInstrumentDSInfo.UserPwd};";
  30. string sql = string.Format(@"SELECT
  31. c.name AS ColumnName,
  32. t.name AS DataType,
  33. c.max_length AS MaxLength,
  34. c.is_nullable AS IsNullable,
  35. c.default_object_id AS DefaultObjectId,
  36. ep.value AS remark
  37. FROM
  38. sys.columns c
  39. JOIN
  40. sys.types t ON c.user_type_id = t.user_type_id
  41. LEFT JOIN
  42. sys.extended_properties ep
  43. ON
  44. c.object_id = ep.major_id
  45. AND c.column_id = ep.minor_id
  46. AND ep.name = 'MS_Description' -- 备注的属性名称
  47. WHERE
  48. c.object_id = OBJECT_ID('{0}'); ", strTableName); //查询字符串
  49. dt = GetDataTable(sql, new SqlParameter[] { });
  50. }
  51. catch (Exception ex)
  52. {
  53. //发生异常,写入日志
  54. AppLog.Error(ex.Message);
  55. }
  56. return dt;
  57. }
  58. /// <summary>
  59. /// 查询操作
  60. /// </summary>
  61. /// <param name="sql"></param>
  62. /// <returns></returns>
  63. public static DataTable GetDataTable(string sql, params SqlParameter[] sp)
  64. {
  65. using (SqlConnection conn = new SqlConnection(connectionStr))
  66. {
  67. conn.Open();
  68. using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
  69. {
  70. sda.SelectCommand.Parameters.AddRange(sp);
  71. DataTable dt = new DataTable();
  72. sda.Fill(dt);
  73. return dt;
  74. }
  75. }
  76. }
  77. public static DataTable PostgreSql(string strTableName)
  78. {
  79. DataTable dt = new DataTable();
  80. string strSql = string.Format(@"SELECT
  81. a.attname as ColumnName,
  82. format_type(a.atttypid, a.atttypmod) as DataType,
  83. a.attnotnull as 非空,
  84. col_description(a.attrelid, a.attnum) as remark
  85. FROM
  86. pg_class as c, pg_attribute as a
  87. where
  88. a.attrelid = c.oid
  89. and
  90. a.attnum > 0
  91. and
  92. c.relname = '{0}'; ", strTableName);
  93. try
  94. {
  95. dt = PostgreSQLHelper.ExecuteDataSet(strSql).Tables[0];
  96. }
  97. catch (Exception ex)
  98. {
  99. AppLog.Error(ex.Message);
  100. }
  101. return dt;
  102. }
  103. public static DataTable DmSql(string strTableName)
  104. {
  105. DataTable dt = new DataTable();
  106. string strSql = string.Format(@"SELECT
  107. c.COLUMN_NAME AS ColumnName,
  108. c.DATA_TYPE AS DataType,
  109. c.DATA_LENGTH AS 字段长度,
  110. c.NULLABLE AS 是否允许为空,
  111. com.COMMENTS AS remark
  112. FROM
  113. USER_TAB_COLUMNS c
  114. LEFT JOIN
  115. USER_COL_COMMENTS com
  116. ON
  117. c.TABLE_NAME = com.TABLE_NAME
  118. AND c.COLUMN_NAME = com.COLUMN_NAME
  119. WHERE
  120. c.TABLE_NAME = '{0}';", strTableName);
  121. try
  122. {
  123. dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
  124. }
  125. catch (Exception ex)
  126. {
  127. AppLog.Error(ex.Message);
  128. }
  129. return dt;
  130. }
  131. public static DataTable OrcSql(string strTableName, SyncInstrumentItemInfo t) {
  132. connectionStr = $"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={t.SyncInstrumentDSInfo.Host})(PORT={t.SyncInstrumentDSInfo.Port}))" + $"(CONNECT_DATA=(SID={t.SyncInstrumentDSInfo.ServerName})));User Id={t.SyncInstrumentDSInfo.UserId};Password={t.SyncInstrumentDSInfo.UserPwd};";
  133. DataTable dt = new DataTable();
  134. string strSql = string.Format("select COLUMN_NAME AS ColumnName,NULLABLE AS IsNullable,DATA_TYPE AS DataType,DATA_LENGTH AS CharMaxLenth,DATA_LENGTH AS CharOcterLenth,DATA_PRECISION AS NumericPrecision,DATA_SCALE AS NumericScale from user_tab_columns where table_name='{0}'", strTableName.ToUpper());
  135. try
  136. {
  137. dt = GetDataTable(strSql, new OracleParameter[] { });
  138. }
  139. catch (Exception ex)
  140. {
  141. AppLog.Error(ex.Message);
  142. }
  143. return dt;
  144. }
  145. /// <summary>
  146. /// 查询操作
  147. /// </summary>
  148. /// <param name="sql"></param>
  149. /// <returns></returns>
  150. public static DataTable GetDataTable(string sql, params OracleParameter[] sp)
  151. {
  152. DataTable dt = new DataTable();
  153. try
  154. {
  155. using (OracleConnection conn = new OracleConnection(connectionStr))
  156. {
  157. conn.Open();
  158. //using (OracleDataAdapter sda = new OracleDataAdapter("select * from t_weight_info where WEIGH_DATE > '2019-10-02 14:20:12'", conn))
  159. using (OracleDataAdapter sda = new OracleDataAdapter(sql, conn))
  160. {
  161. sda.SelectCommand.Parameters.AddRange(sp);
  162. sda.Fill(dt);
  163. }
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. AppLog.Error(ex.Message);
  169. }
  170. return dt;
  171. }
  172. }
  173. }