PostgresqlHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Npgsql;
  6. using System.Data;
  7. namespace Aitex.Core.RT.DBCore
  8. {
  9. public class PostgresqlHelper
  10. {
  11. /// <summary>
  12. /// 连接字符串需要初始化
  13. /// </summary>
  14. public static string ConnectionString { get; set; }
  15. /// <summary>
  16. /// 获得连接对象
  17. /// </summary>
  18. /// <returns></returns>
  19. public static NpgsqlConnection GetConnection()
  20. {
  21. if (string.IsNullOrWhiteSpace(ConnectionString))
  22. throw new ArgumentNullException("PostgresqlConn");
  23. return new NpgsqlConnection(ConnectionString);
  24. }
  25. static void PrepareCommand(NpgsqlCommand cmd, NpgsqlConnection conn, string cmdText, params object[] p)
  26. {
  27. if (conn.State != ConnectionState.Open)
  28. conn.Open();
  29. cmd.Parameters.Clear();
  30. cmd.Connection = conn;
  31. cmd.CommandText = cmdText;
  32. cmd.CommandType = CommandType.Text;
  33. if (p != null)
  34. {
  35. foreach (object parm in p)
  36. cmd.Parameters.AddWithValue(string.Empty, parm);
  37. }
  38. }
  39. public static DataSet ExecuteDataset(string cmdText, params object[] p)
  40. {
  41. DataSet ds = new DataSet();
  42. using (NpgsqlConnection connection = GetConnection())
  43. using (NpgsqlCommand command = new NpgsqlCommand())
  44. {
  45. PrepareCommand(command, connection, cmdText, p);
  46. NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
  47. da.Fill(ds);
  48. }
  49. return ds;
  50. }
  51. public static DataRow ExecuteDataRow(string cmdText, params object[] p)
  52. {
  53. DataSet ds = ExecuteDataset(cmdText, p);
  54. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  55. return ds.Tables[0].Rows[0];
  56. return null;
  57. }
  58. /// <summary>
  59. /// 返回受影响的行数
  60. /// </summary>
  61. public static int ExecuteNonQuery(string cmdText, params object[] p)
  62. {
  63. using (NpgsqlConnection connection = GetConnection())
  64. using (NpgsqlCommand command = new NpgsqlCommand())
  65. {
  66. PrepareCommand(command, connection, cmdText, p);
  67. return command.ExecuteNonQuery();
  68. }
  69. }
  70. /// <summary>
  71. /// 返回NpgsqlDataReader对象
  72. /// </summary>
  73. public static NpgsqlDataReader ExecuteReader(string cmdText, params object[] p)
  74. {
  75. NpgsqlConnection connection = GetConnection();
  76. NpgsqlCommand command = new NpgsqlCommand();
  77. try
  78. {
  79. PrepareCommand(command, connection, cmdText, p);
  80. return command.ExecuteReader(CommandBehavior.CloseConnection);
  81. }
  82. catch
  83. {
  84. connection.Close();
  85. throw;
  86. }
  87. }
  88. /// <summary>
  89. /// 返回结果集中的第一行第一列,忽略其他行或列
  90. /// </summary>
  91. public static object ExecuteScalar(string cmdText, params object[] p)
  92. {
  93. using (NpgsqlConnection connection = GetConnection())
  94. using (NpgsqlCommand command = new NpgsqlCommand())
  95. {
  96. PrepareCommand(command, connection, cmdText, p);
  97. return command.ExecuteScalar();
  98. }
  99. }
  100. /// <summary>
  101. /// 分页
  102. /// </summary>
  103. public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, params object[] p)
  104. {
  105. if (recordCount < 0)
  106. recordCount = int.Parse(ExecuteScalar(countText, p).ToString());
  107. DataSet ds = new DataSet();
  108. using(NpgsqlConnection connection = GetConnection())
  109. using(NpgsqlCommand command = new NpgsqlCommand())
  110. {
  111. PrepareCommand(command, connection, cmdText, p);
  112. NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
  113. da.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result");
  114. }
  115. return ds;
  116. }
  117. }
  118. }