NpgsqlHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Npgsql;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WWPipeLine.Commons;
  9. namespace WWPipeLine.MapBasic
  10. {
  11. public class NpgsqlHelper
  12. {
  13. public NpgsqlHelper() { }
  14. //HOST=39.99.237.110;PORT=5432;DATABASE=weiweiGIS110;USER ID=postgres;PASSWORD=wwkj@2136807;
  15. //ComsStatic.getMd5Hash("", "ww");
  16. private static string ConStr = IniHelper.Read(IniHelper.KEY_ConfigsNpgsql_PATH);
  17. public int ExecuteNonQuery(string sql)
  18. {
  19. int result = -1;
  20. using (NpgsqlConnection connection = new NpgsqlConnection(ConStr))
  21. {
  22. using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
  23. {
  24. try
  25. {
  26. connection.Open();
  27. result = command.ExecuteNonQuery();
  28. }
  29. catch (NpgsqlException exception)
  30. {
  31. LogHelper.Error(string.Format("执行SQL[{0}]出错,详细信息为:{1}", sql, exception.Message));
  32. }
  33. finally
  34. {
  35. connection.Close();
  36. }
  37. }
  38. }
  39. return result;
  40. }
  41. public object ExecuteScalar(string sql)
  42. {
  43. object obj = null;
  44. using (NpgsqlConnection connection = new NpgsqlConnection(ConStr))
  45. {
  46. using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
  47. {
  48. try
  49. {
  50. connection.Open();
  51. obj = command.ExecuteScalar();
  52. }
  53. catch (NpgsqlException exception)
  54. {
  55. LogHelper.Error(string.Format("执行SQL[{0}]出错,详细信息为:{1}", sql, exception.Message));
  56. }
  57. finally
  58. {
  59. connection.Close();
  60. }
  61. }
  62. }
  63. return obj;
  64. }
  65. public DataSet ExecuteQuery(string sql)
  66. {
  67. DataSet result = new DataSet();
  68. using (NpgsqlConnection connection = new NpgsqlConnection(ConStr))
  69. {
  70. using (NpgsqlDataAdapter dataAdapter = new NpgsqlDataAdapter(sql, connection))
  71. {
  72. try
  73. {
  74. connection.Open();
  75. dataAdapter.Fill(result);
  76. }
  77. catch (NpgsqlException exception)
  78. {
  79. LogHelper.Error(string.Format("执行SQL【{0}】出错,详细信息为:{1}", sql, exception.Message));
  80. }
  81. finally
  82. {
  83. connection.Close();
  84. }
  85. }
  86. }
  87. return result;
  88. }
  89. }
  90. }