Then, in Database.cs, remove all of the template code and replace it with the following code: using In Solution Pad, in the LocalDatabaseTutorial project, add a new class named Database to the project. Then, in Person.cs, remove all of the template code and replace it with the following code: using SQLite In Solution Pad, in the LocalDatabaseTutorial project, add a new class named Person to the project. The advantage of exposing the database as a singleton is that a single database connection is created that's kept open while the application runs, therefore avoiding the expense of opening and closing the database file each time a database operation is performed.īuild the solution to ensure there are no errors. A local file path and filename, which represents where to store the database, are passed as the argument to the Database class constructor. This code defines a Database property that creates a new Database instance as a singleton. Then, in, remove all of the template code and replace it with the following code: using System ĭatabase = new Database(Path.Combine(Environment.GetFolderPath(), "people.db3")) In Solution Explorer, in the LocalDatabaseTutorial project, expand App.xaml and double-click to open it. This path will be provided by the App class in the next exercise. In addition, the Database constructor takes the path of the database file as an argument. The code uses asynchronous SQLite.NET APIs that move database operations to background threads. This class contains code to create the database, read data from it, and write data to it. Public Task SavePersonAsync(Person person) _database = new SQLiteAsyncConnection(dbPath) Readonly SQLiteAsyncConnection _database In Solution Explorer, in the LocalDatabaseTutorial project, add a new class named Database to the project. The ID property is marked with PrimaryKey and AutoIncrement attributes to ensure that each Person instance in the database will have a unique id provided by SQLite.NET. This code defines a Person class that will store data about each person in the application. In Solution Explorer, in the LocalDatabaseTutorial project, add a new class named Person to the project.
0 Comments
Leave a Reply. |