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.

319 line
11KB

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