Database Connection Class


/ Published in: C#
Save to your folder(s)

Database connection class to be easily incorporated into any db based softwares, using SQLite 3


Copy this code and paste it in your HTML
  1. class DatabaseConnection
  2. {
  3. private SQLiteConnection conn;
  4. private SQLiteCommand sqlCommand;
  5. private SQLiteDataAdapter DB;
  6. private DataSet DS = new DataSet();
  7.  
  8. public DatabaseConnection()
  9. {
  10. conn = new SQLiteConnection("DataSource=ProfilerPro.s3db;New=False;Compress=True;");
  11. }
  12.  
  13. public SQLiteConnection Openconn()
  14. {
  15. if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
  16. {
  17. conn.Open();
  18. }
  19. return conn;
  20. }
  21.  
  22. public SQLiteConnection Closeconn()
  23. {
  24. if (conn.State == ConnectionState.Open)
  25. {
  26. conn.Close();
  27. }
  28. return conn;
  29. }
  30.  
  31. public void ExecuteQuery(string txtQuery)
  32. {
  33.  
  34.  
  35. SQLiteCommand cmd = new SQLiteCommand();
  36. try
  37. {
  38. cmd.Connection = Openconn();
  39. cmd.CommandText = txtQuery;
  40. cmd.ExecuteNonQuery();
  41. }
  42. catch (Exception Ex)
  43. {
  44. throw Ex;
  45. }
  46. finally
  47. {
  48. cmd = null;
  49. }
  50. }
  51.  
  52. public DataSet returnDataSet(string txtQuery)
  53. {
  54. conn.Open();
  55. sqlCommand = conn.CreateCommand();
  56. DB = new SQLiteDataAdapter(txtQuery, conn);
  57. DS.Reset();
  58. DB.Fill(DS);
  59. conn.Close();
  60. return (DS);
  61. }
  62.  
  63.  
  64. public DataTable returnDataTable(string txtQuery)
  65. {
  66. SQLiteCommand cmd = new SQLiteCommand();
  67. SQLiteDataAdapter adap;
  68. try
  69. {
  70. cmd.Connection = Openconn();
  71. cmd.CommandText = txtQuery;
  72. adap = new SQLiteDataAdapter(cmd);
  73. DataTable dt = new DataTable();
  74. adap.Fill(dt);
  75.  
  76. return dt;
  77. }
  78. catch (Exception Ex)
  79. {
  80. throw Ex;
  81. }
  82. finally
  83. {
  84. cmd = null;
  85. }
  86. }
  87.  
  88. public SQLiteDataReader returnDataReader(string txtQuery)
  89. {
  90. SQLiteCommand cmd = new SQLiteCommand();
  91. try
  92. {
  93. cmd.Connection = Openconn();
  94. cmd.CommandText = txtQuery;
  95. SQLiteDataReader rd;
  96. rd = cmd.ExecuteReader();
  97. return rd;
  98. }
  99. catch (Exception Ex)
  100. {
  101. throw Ex;
  102. }
  103. finally
  104. {
  105. cmd = null;
  106. }
  107. }
  108. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.