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.

305 line
11KB

  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. #region 自动模式调取方法
  18. public static DataTable MySqlsec(string strTableName)
  19. {
  20. MySQLDAL mysql = new MySQLDAL();
  21. DataTable tb = mysql.GetTableTypeAndLenth(strTableName);
  22. return tb;
  23. }
  24. public static DataTable Sqlserversec(string strTableName, SyncInstrumentItemInfo t)
  25. {
  26. DataTable dt = new DataTable();
  27. try
  28. {
  29. if (t.SyncInstrumentDSInfo.Host != "")
  30. connectionStr = $"Data Source = {t.SyncInstrumentDSInfo.Host}; Initial Catalog = {t.SyncInstrumentDSInfo.ServerName}; User Id = {t.SyncInstrumentDSInfo.UserId}; Password = {t.SyncInstrumentDSInfo.UserPwd};";
  31. string sql = string.Format(@"SELECT
  32. c.name AS ColumnName,
  33. t.name AS DataType,
  34. c.max_length AS MaxLength,
  35. c.is_nullable AS IsNullable,
  36. c.default_object_id AS DefaultObjectId,
  37. ep.value AS remark
  38. FROM
  39. sys.columns c
  40. JOIN
  41. sys.types t ON c.user_type_id = t.user_type_id
  42. LEFT JOIN
  43. sys.extended_properties ep
  44. ON
  45. c.object_id = ep.major_id
  46. AND c.column_id = ep.minor_id
  47. AND ep.name = 'MS_Description' -- 备注的属性名称
  48. WHERE
  49. c.object_id = OBJECT_ID('{0}'); ", strTableName); //查询字符串
  50. dt = GetDataTable(sql, new SqlParameter[] { });
  51. }
  52. catch (Exception ex)
  53. {
  54. //发生异常,写入日志
  55. AppLog.Error(ex.Message);
  56. }
  57. return dt;
  58. }
  59. /// <summary>
  60. /// 查询操作
  61. /// </summary>
  62. /// <param name="sql"></param>
  63. /// <returns></returns>
  64. public static DataTable GetDataTable(string sql, params SqlParameter[] sp)
  65. {
  66. using (SqlConnection conn = new SqlConnection(connectionStr))
  67. {
  68. conn.Open();
  69. using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
  70. {
  71. sda.SelectCommand.Parameters.AddRange(sp);
  72. DataTable dt = new DataTable();
  73. sda.Fill(dt);
  74. return dt;
  75. }
  76. }
  77. }
  78. public static DataTable PostgreSql(string strTableName)
  79. {
  80. DataTable dt = new DataTable();
  81. string strSql = string.Format(@"SELECT
  82. a.attname as ColumnName,
  83. format_type(a.atttypid, a.atttypmod) as DataType,
  84. a.attnotnull as 非空,
  85. col_description(a.attrelid, a.attnum) as remark
  86. FROM
  87. pg_class as c, pg_attribute as a
  88. where
  89. a.attrelid = c.oid
  90. and
  91. a.attnum > 0
  92. and
  93. c.relname = '{0}'; ", strTableName);
  94. try
  95. {
  96. dt = PostgreSQLHelper.ExecuteDataSet(strSql).Tables[0];
  97. }
  98. catch (Exception ex)
  99. {
  100. AppLog.Error(ex.Message);
  101. }
  102. return dt;
  103. }
  104. public static DataTable DmSql(string strTableName)
  105. {
  106. DataTable dt = new DataTable();
  107. string strSql = string.Format(@"SELECT
  108. c.COLUMN_NAME AS ColumnName,
  109. c.DATA_TYPE AS DataType,
  110. c.DATA_LENGTH AS 字段长度,
  111. c.NULLABLE AS 是否允许为空,
  112. com.COMMENTS AS remark
  113. FROM
  114. USER_TAB_COLUMNS c
  115. LEFT JOIN
  116. USER_COL_COMMENTS com
  117. ON
  118. c.TABLE_NAME = com.TABLE_NAME
  119. AND c.COLUMN_NAME = com.COLUMN_NAME
  120. WHERE
  121. c.TABLE_NAME = '{0}';", strTableName);
  122. try
  123. {
  124. dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
  125. }
  126. catch (Exception ex)
  127. {
  128. AppLog.Error(ex.Message);
  129. }
  130. return dt;
  131. }
  132. public static DataTable OrcSql(string strTableName, SyncInstrumentItemInfo t) {
  133. 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};";
  134. DataTable dt = new DataTable();
  135. 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());
  136. try
  137. {
  138. dt = GetDataTable(strSql, new OracleParameter[] { });
  139. }
  140. catch (Exception ex)
  141. {
  142. AppLog.Error(ex.Message);
  143. }
  144. return dt;
  145. }
  146. /// <summary>
  147. /// 查询操作
  148. /// </summary>
  149. /// <param name="sql"></param>
  150. /// <returns></returns>
  151. public static DataTable GetDataTable(string sql, params OracleParameter[] sp)
  152. {
  153. DataTable dt = new DataTable();
  154. try
  155. {
  156. using (OracleConnection conn = new OracleConnection(connectionStr))
  157. {
  158. conn.Open();
  159. //using (OracleDataAdapter sda = new OracleDataAdapter("select * from t_weight_info where WEIGH_DATE > '2019-10-02 14:20:12'", conn))
  160. using (OracleDataAdapter sda = new OracleDataAdapter(sql, conn))
  161. {
  162. sda.SelectCommand.Parameters.AddRange(sp);
  163. sda.Fill(dt);
  164. }
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. AppLog.Error(ex.Message);
  170. }
  171. return dt;
  172. }
  173. public static DataTable KingSql(string strTableName)
  174. {
  175. DataTable dt = new DataTable();
  176. string strSql = string.Format(" \r\n SELECT \r\n a.attname as ColumnName,\r\n \r\n col_description(a.attrelid, a.attnum) as remark,\r\n b.data_type AS DATATYPE \r\n \r\nFROM \r\n pg_class as c,\r\n pg_attribute as a,\r\n information_schema.COLUMNS b\r\n \t\r\nWHERE \r\n c.relname = 'stu' \r\n and a.attrelid = c.oid \r\n AND a.attname=b.column_name\r\n \tAND b. table_name LIKE '{0}'\r\n", strTableName);
  177. try
  178. {
  179. dt = KingbaseHelper.ExecuteDataSet(strSql).Tables[0];
  180. }
  181. catch (Exception ex)
  182. {
  183. AppLog.Error(ex.Message);
  184. }
  185. return dt;
  186. }
  187. #endregion
  188. #region 手动模式调取方法
  189. public static DataTable MySqlsecSD(string strSql)
  190. {
  191. DataTable dt = new DataTable();
  192. try
  193. {
  194. dt = MySQLHelper.ExecuteDataSet(strSql).Tables[0];
  195. }
  196. catch (Exception ex)
  197. {
  198. AppLog.Error(ex.Message);
  199. }
  200. return dt;
  201. }
  202. public static DataTable SqlserversecSD(string sql, SyncInstrumentItemInfo t)
  203. {
  204. DataTable dt = new DataTable();
  205. try
  206. {
  207. if (t.SyncInstrumentDSInfo.Host != "")
  208. connectionStr = $"Data Source = {t.SyncInstrumentDSInfo.Host}; Initial Catalog = {t.SyncInstrumentDSInfo.ServerName}; User Id = {t.SyncInstrumentDSInfo.UserId}; Password = {t.SyncInstrumentDSInfo.UserPwd};";
  209. dt = GetDataTable(sql, new SqlParameter[] { });
  210. }
  211. catch (Exception ex)
  212. {
  213. //发生异常,写入日志
  214. AppLog.Error(ex.Message);
  215. }
  216. return dt;
  217. }
  218. public static DataTable PostgreSqlSD(string strSql)
  219. {
  220. DataTable dt = new DataTable();
  221. try
  222. {
  223. dt = PostgreSQLHelper.ExecuteDataSet(strSql).Tables[0];
  224. }
  225. catch (Exception ex)
  226. {
  227. AppLog.Error(ex.Message);
  228. }
  229. return dt;
  230. }
  231. public static DataTable DmSqlSD(string strSql)
  232. {
  233. DataTable dt = new DataTable();
  234. try
  235. {
  236. dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
  237. }
  238. catch (Exception ex)
  239. {
  240. AppLog.Error(ex.Message);
  241. }
  242. return dt;
  243. }
  244. public static DataTable OrcSqlSD(string strSql, SyncInstrumentItemInfo t)
  245. {
  246. 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};";
  247. DataTable dt = new DataTable();
  248. // 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());
  249. try
  250. {
  251. dt = GetDataTable(strSql, new OracleParameter[] { });
  252. }
  253. catch (Exception ex)
  254. {
  255. AppLog.Error(ex.Message);
  256. }
  257. return dt;
  258. }
  259. public static DataTable KingSqlSD(string Sql)
  260. {
  261. DataTable dt = new DataTable();
  262. //string strSql = string.Format(" SELECT a.attname as ColumnName,col_description(a.attrelid, a.attnum) as remark, b.data_type AS DATATYPE FROM pg_class as c, pg_attribute as a, information_schema.COLUMNS b WHERE c.relname = '{0}' and a.attrelid = c.oid AND a.attname=b.column_name AND b. table_name LIKE '{0}'", strTableName);
  263. try
  264. {
  265. dt = KingbaseHelper.ExecuteDataSet(Sql).Tables[0];
  266. }
  267. catch (Exception ex)
  268. {
  269. AppLog.Error(ex.Message);
  270. }
  271. return dt;
  272. }
  273. #endregion
  274. }
  275. }