public static List <Note> ReadNotes() { var notes = new List <Note> (); var connection = NoteDBUtil.CreateConnnection(); using (var cmd = connection.CreateCommand()) { connection.Open(); string sql = "Select * From Note"; cmd.CommandText = sql; using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { long id = (long)reader["id"]; string title = (string)reader["title"]; string body = (string)reader["body"]; notes.Add(new Note(id, title, body)); } } connection.Close(); } return(notes); }
void CopyDBToDocuments() { //Files in app bundle are read only, so you must copy //the database file to a writeable location, such as the //documents directory, in order to write to the db. string dbPath = NoteDBUtil.GetDBPath(); if (!File.Exists(dbPath)) { File.Copy("MTNotesDB.sqlite", dbPath); } }
public void Delete() { using (var connection = NoteDBUtil.CreateConnnection()) { using (var cmd = connection.CreateCommand()) { connection.Open(); string sql = "Delete From Note Where id=@id"; SqliteParameter idParam = new SqliteParameter("@id", _id); cmd.Parameters.Add(idParam); cmd.CommandText = sql; cmd.ExecuteNonQuery(); connection.Close(); } } }
public void Save() { using (var connection = NoteDBUtil.CreateConnnection()) { using (var cmd = connection.CreateCommand()) { connection.Open(); if (_id < 0) { InsertNote(cmd); } else { UpdateNote(cmd); } connection.Close(); } } }