NpgsqlHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using log4net;
  2. using Npgsql;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace TimedUpload
  13. {
  14. class NpgsqlHelper
  15. {
  16. private static readonly ILog log = LogManager.GetLogger(typeof(NpgsqlHelper));
  17. private static string ConStr = ConfigurationManager.AppSettings["dbNpgsql"];
  18. public static int ExecuteNonQuery(string sql)
  19. {
  20. int result = -1;
  21. using (NpgsqlConnection connection = new NpgsqlConnection(ConStr))
  22. {
  23. using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
  24. {
  25. try
  26. {
  27. connection.Open();
  28. result = command.ExecuteNonQuery();
  29. log.Info(sql);
  30. }
  31. catch (NpgsqlException exception)
  32. {
  33. log.Error(string.Format("执行SQL[{0}]出错,详细信息为:{1}", sql, exception.Message));
  34. }
  35. finally
  36. {
  37. connection.Close();
  38. }
  39. }
  40. }
  41. return result;
  42. }
  43. public static object ExecuteScalar(string sql)
  44. {
  45. object obj = null;
  46. using (NpgsqlConnection connection = new NpgsqlConnection(ConStr))
  47. {
  48. using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
  49. {
  50. try
  51. {
  52. connection.Open();
  53. obj = command.ExecuteScalar();
  54. log.Info(sql);
  55. }
  56. catch (NpgsqlException exception)
  57. {
  58. obj = -1;
  59. log.Error(string.Format("执行SQL[{0}]出错,详细信息为:{1}", sql, exception.Message));
  60. }
  61. finally
  62. {
  63. connection.Close();
  64. }
  65. }
  66. }
  67. return obj;
  68. }
  69. public static DataSet ExecuteQuery(string sql)
  70. {
  71. DataSet result = new DataSet();
  72. using (NpgsqlConnection connection = new NpgsqlConnection(ConStr))
  73. {
  74. using (NpgsqlDataAdapter dataAdapter = new NpgsqlDataAdapter(sql, connection))
  75. {
  76. try
  77. {
  78. connection.Open();
  79. dataAdapter.Fill(result);
  80. log.Info(sql);
  81. }
  82. catch (NpgsqlException exception)
  83. {
  84. log.Error(string.Format("执行SQL【{0}】出错,详细信息为:{1}", sql, exception.Message));
  85. }
  86. finally
  87. {
  88. connection.Close();
  89. }
  90. }
  91. }
  92. return result;
  93. }
  94. public static string DesDecrypt(string decryptString, string key)
  95. {
  96. byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
  97. byte[] keyIV = keyBytes;
  98. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  99. DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
  100. MemoryStream mStream = new MemoryStream();
  101. CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
  102. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  103. cStream.FlushFinalBlock();
  104. return Encoding.UTF8.GetString(mStream.ToArray());
  105. }
  106. }
  107. }