DBHelper.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data.OleDb;
  5. using System.Data;
  6. using System.IO;
  7. namespace BooksManageSystem
  8. {
  9. public class DBHelper
  10. {
  11. private static string conStr ;//= @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DB.accdb";
  12. static DBHelper()
  13. {
  14. conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"DB.accdb") + "\"";
  15. }
  16. //OleDbCommand comm = new OleDbCommand();
  17. public static DataTable ExecuteDataTable(OleDbCommand comm)
  18. {
  19. using (OleDbConnection conn = new OleDbConnection(conStr))
  20. {
  21. comm.Connection = conn;
  22. DataTable dt = new DataTable();
  23. OleDbDataAdapter adpt = new OleDbDataAdapter(comm);
  24. adpt.Fill(dt);
  25. return dt;
  26. }
  27. }
  28. public static object ExecuteScalar(OleDbCommand comm)
  29. {
  30. using (OleDbConnection conn = new OleDbConnection(conStr))
  31. {
  32. comm.Connection = conn;
  33. if (conn.State != ConnectionState.Open)
  34. {
  35. conn.Open();
  36. }
  37. return comm.ExecuteScalar();
  38. }
  39. }
  40. public static int ExecuteNonQuery(OleDbCommand comm)
  41. {
  42. using (OleDbConnection conn = new OleDbConnection(conStr))
  43. {
  44. comm.Connection = conn;
  45. conn.Open();
  46. return comm.ExecuteNonQuery();
  47. }
  48. }
  49. public static int ExecuteNonQuery(string sql)
  50. {
  51. using (OleDbCommand comm = new OleDbCommand(sql))
  52. {
  53. return ExecuteNonQuery(comm);
  54. }
  55. }
  56. }
  57. }