Helper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.OleDb;
  6. using System.Data;
  7. using System.IO;
  8. namespace AVSORTER.DB
  9. {
  10. public class Helper
  11. {
  12. string b = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db","AVDB.accdb");
  13. string ConnectString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
  14. OleDbConnection connection;
  15. /// <summary>
  16. /// 数据连接帮助类
  17. /// </summary>
  18. /// <param name="connectionString">为空字符时使用默认的连接字符串</param>
  19. public Helper(string connectionString)
  20. {
  21. ConnectString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + b;
  22. if (connectionString== string.Empty)
  23. {
  24. connection = new OleDbConnection(ConnectString);
  25. }
  26. else
  27. {
  28. connection = new OleDbConnection(connectionString);
  29. }
  30. try
  31. {
  32. connection.Open();
  33. connection.Close();
  34. }
  35. catch (Exception err)
  36. {
  37. throw err;
  38. }
  39. }
  40. public DataTable ExecuteDataTable(OleDbCommand command)
  41. {
  42. AddConnection(command);
  43. OleDbDataAdapter adpt = new OleDbDataAdapter(command);
  44. DataTable datatable = new DataTable();
  45. adpt.Fill(datatable);
  46. return datatable;
  47. }
  48. public DataTable ExecuteDataTable(string selectCommand)
  49. {
  50. OleDbDataAdapter adpt = new OleDbDataAdapter(selectCommand,this.connection);
  51. DataTable datatable = new DataTable();
  52. adpt.Fill(datatable);
  53. return datatable;
  54. }
  55. /// <summary>
  56. /// in used
  57. /// </summary>
  58. /// <param name="command"></param>
  59. /// <returns></returns>
  60. public int ExecuteNonQuery(OleDbCommand command)
  61. {
  62. AddConnection(command);
  63. using (command)
  64. {
  65. if (command.Connection.State == ConnectionState.Closed)
  66. {
  67. command.Connection.Open();
  68. }
  69. return command.ExecuteNonQuery();
  70. }
  71. }
  72. public int ExecuteNonQuery(string nonQueryCommand)
  73. {
  74. using (OleDbCommand comm = new OleDbCommand(nonQueryCommand))
  75. {
  76. AddConnection(comm);
  77. return comm.ExecuteNonQuery();
  78. }
  79. }
  80. public object ExecuteScalar(OleDbCommand command)
  81. {
  82. AddConnection(command);
  83. using (command)
  84. {
  85. if (command.Connection.State == ConnectionState.Closed)
  86. {
  87. command.Connection.Open();
  88. }
  89. return command.ExecuteScalar();
  90. }
  91. }
  92. public object ExecuteScalar(string sqlCommand)
  93. {
  94. using (OleDbCommand comm = new OleDbCommand(sqlCommand))
  95. {
  96. AddConnection(comm);
  97. if (comm.Connection.State == ConnectionState.Closed)
  98. {
  99. comm.Connection.Open();
  100. }
  101. return comm.ExecuteScalar();
  102. }
  103. }
  104. #region 属性
  105. public ConnectionState State
  106. {
  107. get { return this.connection.State; }
  108. }
  109. #endregion
  110. private void AddConnection(OleDbCommand comm)
  111. {
  112. comm.Connection = this.connection;
  113. }
  114. }
  115. }