Created
July 24, 2019 10:20
-
-
Save orlodax/4955f65a4f60a4b9d0a9f4241fdf8d5e to your computer and use it in GitHub Desktop.
Read SQLite table and store results inside DataTable C# UWP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DataTable ExecuteReadQuery(string query) | |
| { | |
| DataTable entries = new DataTable(); | |
| using (SqliteConnection db = new SqliteConnection(ConnectionString)) | |
| { | |
| SqliteCommand selectCommand = new SqliteCommand(query, db); | |
| try | |
| { | |
| db.Open(); | |
| SqliteDataReader reader = selectCommand.ExecuteReader(); | |
| if (reader.HasRows) | |
| for (int i = 0; i < reader.FieldCount; i++) | |
| entries.Columns.Add(new DataColumn(reader.GetName(i))); | |
| int j = 0; | |
| while (reader.Read()) | |
| { | |
| DataRow row = entries.NewRow(); | |
| entries.Rows.Add(row); | |
| for (int i = 0; i < reader.FieldCount; i++) | |
| entries.Rows[j][i] = (reader.GetValue(i)); | |
| j++; | |
| } | |
| db.Close(); | |
| } | |
| catch (SqliteException e) | |
| { | |
| OnSQLiteError(new SQLiteErrorEventArgs(e)); | |
| db.Close(); | |
| } | |
| return entries; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @stefanvesely !