L Hazlewood

L Hazlewood

  • NA
  • 23
  • 1k

SQLite and Windows 2017 C# query of database file

Mar 15 2018 6:15 AM
In this section of code I select a range of dates to use to get the rows from a SQLite database file "Mytable1 table name "Date". The SELECT command I am using works correctly when using a SQLite database manager program but using C# in a windows form the dates returned are not between the date range requested.
 
Anyone run into this issue before?
 
I use a DataView Grid to display the return data and a button to SELECT the table and "Date coloum. A DataBase file (.zip) and a few lines of code I am using.
  1. using System.Data.SQLite;  
  2. //SQL connection, Adaptor and data table  
  3. SQLiteConnection myConn;  
  4. SQLiteDataAdapter myAdapter;  
  5. DataTable myTable;  
  6. // Loads page and opens data base  
  7. private void Reports_Load_1(object sender, EventArgs e) // add event load  
  8. {  
  9. // check if file exists  
  10. if (File.Exists(Application.StartupPath + "/mytable1.db3")) // change back to mytables when testing is done  
  11. {  
  12. string myDbPath = Application.StartupPath + "/mytable1.db3"// change back to mytables when testing is done  
  13. // if DB does not exist, it will create it.  
  14. myConn = new SQLiteConnection("Data Source=" + myDbPath);  
  15. myConn.Open(); // open DB file  
  16. }  
  17. else  
  18. {  
  19. MessageBox.Show("Data Base not found:" + Environment.NewLine + // addes a new line  
  20. " Check file location and name");  
  21. }  
  22. }  
  23. private void BtnGenerate_Click(object sender, EventArgs e)  
  24. {  
  25. myAdapter = new SQLiteDataAdapter ("SELECT DISTINCT * FROM Transaction_Table WHERE Date >= "+2018/01/01+" AND Date <= "+2018/01/04+";" , myConn); // this works in SQLite database browser. In visualstudio 2015 C# gives all dates regardless of the date rang you change it to.  
  26. // myAdapter = new SQLiteDataAdapter("SELECT DISTINCT * FROM Transaction_Table WHERE date BETWEEN " + 2018/01/01 + " AND " + 2018/01/04 + ";", myConn); // this also gets all dates  
  27. myTable = new DataTable();  
  28. // myReport = new DataTable(); // Don't forget initialize!  
  29. myAdapter.Fill(myTable); // once you get the data  
  30. // try this change  
  31. // bindingSource1.DataSource = myTable;  
  32. // SQLiteCommandBuilder authomatically generates  
  33. // neccessary INSERT, UPDATE, DELETE SQL queries.  
  34. new SQLiteCommandBuilder(myAdapter);  
  35. // ----------- Binding DataTable To DataGridView -----------  
  36. // ---------------------------------------------------------  
  37. // DataGridView visualizes DataTable's data in the window.  
  38. // ! THIS CODE IS REQUIRED (if we want DataGridView)  
  39. DataGridView1.DataSource = myTable;  
  40. }

Attachment: mytable1.zip

Answers (2)