纽威
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.

383 lines
14 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Reflection;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Data.SqlClient;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Threading.Tasks;
  13. //using NFine.Data.Extensions;
  14. namespace ICSSoft.FromERP
  15. {
  16. public class ICSHelper
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. public static string GetConnectString()
  20. {
  21. try
  22. {
  23. string connectionString = GetConfigString("SysConnectionString");
  24. return connectionString;
  25. }
  26. catch (Exception)
  27. {
  28. throw;
  29. }
  30. }
  31. public static string GetPLMConnectString()
  32. {
  33. try
  34. {
  35. string connectionString = GetConfigString("SysPlmConnectionString");
  36. return connectionString;
  37. }
  38. catch (Exception)
  39. {
  40. throw;
  41. }
  42. }
  43. public static string GetERPConnectString()
  44. {
  45. try
  46. {
  47. string connectionString = GetConfigString("SysErpConnectionString");
  48. return connectionString;
  49. }
  50. catch (Exception)
  51. {
  52. throw;
  53. }
  54. }
  55. public static string GetConfigString(string name)
  56. {
  57. try
  58. {
  59. Configuration config = GetConfig();
  60. string configString = config.ConnectionStrings.ConnectionStrings[name].ConnectionString.ToString();
  61. return configString;
  62. }
  63. catch (Exception)
  64. {
  65. throw;
  66. }
  67. }
  68. public static Dictionary<string, string> GetConfigString()
  69. {
  70. try
  71. {
  72. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  73. Configuration config = GetConfig();
  74. var settings = config.AppSettings.Settings;
  75. foreach (var key in settings.AllKeys)
  76. {
  77. string value = settings[key].Value.ToString();
  78. dictionary.Add(key.ToString(), value);
  79. }
  80. return dictionary;
  81. }
  82. catch (Exception)
  83. {
  84. throw;
  85. }
  86. }
  87. public static Configuration GetConfig()
  88. {
  89. Assembly assembly = Assembly.GetCallingAssembly();
  90. string path = string.Format("{0}.config", assembly.Location);
  91. if (!File.Exists(path))
  92. {
  93. throw new FileNotFoundException(path + "路径下的文件未找到!");
  94. }
  95. try
  96. {
  97. ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
  98. configFile.ExeConfigFilename = path;
  99. Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
  100. return config;
  101. }
  102. catch (Exception)
  103. {
  104. throw;
  105. }
  106. }
  107. public static void Log(string name)
  108. {
  109. string procName = Process.GetCurrentProcess().ProcessName;
  110. using (PerformanceCounter pc = new PerformanceCounter("Process", "Private Bytes", procName))
  111. {
  112. log.Warn(name + "-当前程序内存占用:" + pc.NextValue());
  113. }
  114. long memoryUsed = GC.GetTotalMemory(true);
  115. log.Warn(name + "-内存占用:" + memoryUsed);
  116. }
  117. public static string Time(string Namespace, string Class, string WorkPoint, string sql, string TempTableName)
  118. {
  119. try
  120. {
  121. string value = @"DECLARE @LastTime datetime='2000-01-01'
  122. --
  123. IF NOT EXISTS(SELECT ID FROM ICSERPTime WHERE Namespace='{0}' AND Class='{1}' AND WorkPoint='{2}')
  124. BEGIN
  125. INSERT INTO ICSERPTime VALUES('{0}','{1}',CONVERT(VARCHAR(10),@LastTime,23),CONVERT(VARCHAR(10),@LastTime,24),GETDATE(),'{2}',@LastTime)
  126. END
  127. --
  128. SELECT @LastTime=DateTime FROM ICSERPTime WHERE Namespace='{0}' AND Class='{1}' AND WorkPoint='{2}'
  129. --MTIME字段
  130. {3}
  131. --
  132. SELECT @LastTime=MAX(MTIME) FROM {4}
  133. --
  134. UPDATE ICSERPTime SET Date=CONVERT(VARCHAR(10),@LastTime,23),Time=CONVERT(VARCHAR(10),@LastTime,24),DateTime=@LastTime,MTIME=GETDATE() WHERE Namespace='{0}' AND Class='{1}' AND WorkPoint='{2}'
  135. ";
  136. return string.Format(value, Namespace, Class, WorkPoint, sql, TempTableName);
  137. }
  138. catch (Exception ex)
  139. {
  140. log.Error(ex.ToString());
  141. throw;
  142. }
  143. }
  144. public static string InsertSQL(string TableName, Dictionary<string, string> values)
  145. {
  146. try
  147. {
  148. string col = string.Empty;
  149. string value = string.Empty;
  150. foreach (var item in values)
  151. {
  152. col += item.Key + ",";
  153. value += item.Value + ",";
  154. }
  155. if (!string.IsNullOrWhiteSpace(value))
  156. {
  157. return string.Format("INSERT INTO {0} ({1}) SELECT {2} FROM ", TableName, col.TrimEnd(','), value.TrimEnd(','));
  158. }
  159. return value;
  160. }
  161. catch (Exception ex)
  162. {
  163. log.Error(ex.ToString());
  164. throw;
  165. }
  166. }
  167. public static string UpdateSQL(string TableName, Dictionary<string, string> values)
  168. {
  169. try
  170. {
  171. string value = string.Empty;
  172. foreach (var item in values)
  173. {
  174. value += item.Key +"="+ item.Value + ",";
  175. }
  176. if (!string.IsNullOrWhiteSpace(value))
  177. {
  178. return string.Format("UPDATE {0} SET {1} FROM ", TableName, value.TrimEnd(','));
  179. }
  180. return "";
  181. }
  182. catch (Exception ex)
  183. {
  184. log.Error(ex.ToString());
  185. throw;
  186. }
  187. }
  188. public static void ExecuteDate(string conStr, string sql)
  189. {
  190. try
  191. {
  192. using (SqlConnection con = new SqlConnection(conStr))
  193. {
  194. con.Open();
  195. try
  196. {
  197. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  198. using (SqlTransaction tran = con.BeginTransaction())
  199. {
  200. using (SqlCommand command = new SqlCommand())
  201. {
  202. command.Connection = con;
  203. command.Transaction = tran;
  204. command.CommandTimeout = 100;
  205. command.CommandText = sql;
  206. try
  207. {
  208. int result = command.ExecuteNonQuery();
  209. tran.Commit();
  210. }
  211. catch (Exception ex)
  212. {
  213. tran.Rollback();
  214. log.Error( ex.ToString() + Environment.NewLine + "异常SQL:" + Environment.NewLine + sql);
  215. }
  216. }
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. log.Error(ex.ToString());
  222. throw ex;
  223. }
  224. finally
  225. {
  226. if (con.State == ConnectionState.Open)
  227. con.Close();
  228. con.Dispose();
  229. }
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. log.Error(ex.ToString());
  235. throw ex;
  236. }
  237. }
  238. /// <summary>
  239. /// 获取拼音
  240. /// </summary>
  241. /// <param name="str"></param>
  242. /// <returns></returns>
  243. public static string GetPYString(string str)
  244. {
  245. string tempStr = "";
  246. foreach (char c in str)
  247. {
  248. if ((int)c >= 33 && (int)c <= 126)
  249. {//字母和符号原样保留
  250. tempStr += c.ToString();
  251. }
  252. else
  253. {//累加拼音声母
  254. tempStr += GetPYChar(c.ToString());
  255. }
  256. }
  257. return tempStr;
  258. }
  259. ///
  260. /// 取单个字符的拼音声母
  261. ///
  262. /// 要转换的单个汉字
  263. /// 拼音声母
  264. public static string GetPYChar(string c)
  265. {
  266. byte[] array = new byte[2];
  267. array = System.Text.Encoding.Default.GetBytes(c);
  268. int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
  269. if (i < 0xB0A1) return "*";
  270. if (i < 0xB0C5) return "a";
  271. if (i < 0xB2C1) return "b";
  272. if (i < 0xB4EE) return "c";
  273. if (i < 0xB6EA) return "d";
  274. if (i < 0xB7A2) return "e";
  275. if (i < 0xB8C1) return "f";
  276. if (i < 0xB9FE) return "g";
  277. if (i < 0xBBF7) return "h";
  278. if (i < 0xBFA6) return "j";
  279. if (i < 0xC0AC) return "k";
  280. if (i < 0xC2E8) return "l";
  281. if (i < 0xC4C3) return "m";
  282. if (i < 0xC5B6) return "n";
  283. if (i < 0xC5BE) return "o";
  284. if (i < 0xC6DA) return "p";
  285. if (i < 0xC8BB) return "q";
  286. if (i < 0xC8F6) return "r";
  287. if (i < 0xCBFA) return "s";
  288. if (i < 0xCDDA) return "t";
  289. if (i < 0xCEF4) return "w";
  290. if (i < 0xD1B9) return "x";
  291. if (i < 0xD4D1) return "y";
  292. if (i < 0xD7FA) return "z";
  293. return "*";
  294. }
  295. /// <summary>
  296. /// 返回一个数据表
  297. /// </summary>
  298. /// <param name="connectionString">数据库连接字符串</param>
  299. /// <param name="cmdType">命令类型</param>
  300. /// <param name="cmdText">SQL语句</param>
  301. /// <param name="commandParameters">参数</param>
  302. /// <param name="strTableName">数据表名</param>
  303. /// <param name="bIsLoadStru">是否加载数据表结构</param>
  304. /// <returns></returns>
  305. public static DataTable ExecuteTable(string connection, string cmdText)
  306. {
  307. try
  308. {
  309. using (SqlConnection con = new SqlConnection(connection))
  310. {
  311. con.Open();
  312. try
  313. {
  314. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  315. using (SqlCommand command = new SqlCommand())
  316. {
  317. command.CommandText = cmdText;
  318. DataSet ds = new DataSet();
  319. SqlDataAdapter da = new SqlDataAdapter(command);
  320. da.SelectCommand.Connection = con; //加上这个
  321. //da.FillSchema(ds, SchemaType.Source);
  322. da.Fill(ds);
  323. return ds.Tables[0];
  324. }
  325. }
  326. catch (Exception ex)
  327. {
  328. log.Error(ex.ToString());
  329. throw ex;
  330. }
  331. finally
  332. {
  333. if (con.State == ConnectionState.Open)
  334. con.Close();
  335. con.Dispose();
  336. }
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. log.Error(ex.ToString());
  342. throw ex;
  343. }
  344. }
  345. public static DataTable GetERPDB(string conStr)
  346. {
  347. try
  348. {
  349. string value = @"SELECT * FROM [dbo].[Sys_WorkPoint]";
  350. return ExecuteTable(conStr, value);
  351. }
  352. catch (Exception ex)
  353. {
  354. log.Error(ex.ToString());
  355. throw;
  356. }
  357. }
  358. }
  359. }