How to create a database in Xamarin?
7 minute(s) read | Published on: Mar 24, 2021 Updated on: Dec 14, 2021 |
In this part of the tutorial, you will learn that:
- How to create and add SQLite.NET Use NuGet, Package Manager in Xamarin.Forms projects.
- How to create classes that have access to the data in the Database.
- How to use classes that have access to database data?
The requirements for doing this training are as follows:
- Install the latest version of Visual Studio 2019 on the system
- Install the latest version of Mobile development with .NET on the system
Learn how to create a database using NuGet Package Manager in Xamarin. Forms Visual Studio
1- Open Visual Studio.
2- Create a new black Xamarin.Forms app.
3- Select a name for the created file. The name selected in this article is LocalDatabaseTutorial.
4- In the Solution Explorer, select the LocalDatabasTutorial project and right-click on it.
5- Select Manage NuGetPackeges.
6- In the NuGet Packages Manager section, select the Browse tab.
7- Search for SQLite-net-PCL, install it, and add it to the project.
Note:
The specifications of NuGet packages that are suitable for this tutorial and project are as follows:
- Author (s): Frank A. Krueger
- Id: SQLite-net-PCL
- NuGet link: sqlite-net-plc
Build the solution. Then you can make sure there are no errors.
Learn how to build classes in a database in Xamarin. Forms Visual Studio
1- In the Solution Explorer, create a new class and name it Person.
2- Replace all the codes listed below with the codes in the Person. Cs section.
By entering this code, you create and define a class called Person. The ID attribute is defined as a primary key.
3- In the Solution Explorer section of the LocalDatabaseTutorial project, add a Database class to the project.
4- Replace all the code listed below with the code in the Database. Cs.
This class contains code to create and define a database, read data from it, and write data to it.
Database Constructor receives the address of the database file as an argument.
5- In the Solution Explorer of the LocalDatabeseTutorial project, select the App.XAML section.
6- Double click on App.XAML.cs.
7- Replace all the codes listed below with those in that section.
By entering this code, you define the Database attribute and build a new instance as a singleton.
The advantage of displaying the Database in singleton is that a connection is created, the application is kept open, and we do not need to open and close the file every time we make changes to the Database.
8- Build the solution to make sure there are no errors.
Learn how to use classes to access data in Xamarin.Forms
1- In the Solution Explorer of the LocalDatabaseTutorial project, double-click on the MainPage.XAML section.
2- Replace the codes inserted below with the codes in the file.
By entering this code, you specify a UI for the page, which includes two entries, a Button and a ListView, located inside the StackLayout. Each entry has a placeholder attribute.
An event handler is used to press a Button called OnButtonClicked.
3- In the Solution Explorer of the LocalDatabaseTutorial project, click on the MainPage.XAML section.
4- Double-click on MainPage.xaml.cs.
5- Then add OnAppearing override and OnButtonClicked event handler to this class.
6- Replace the codes inserted below with the codes in the file.
The OnButton Clicked method works when you hit a button and store the data entered in both entries before clearing and refreshing the data in ListView.
7- Press the start or Ctrl + F5 key combination to see the result of the applied changes.
Enter some data and tap the button to save the data to display it in ListView.
The requirements for doing this training are as follows:
- Install the latest version of Visual Studio for Mac on the system
- Install the latest version of the support platform for Android and iOS on the system
- Install the latest version of Xcode on the system
Learn how to make a using NuGet Package Manager in Xamarin.Forms
1- Open Visual Studio for Mac.
2- Create a new black Xamarin.Forms app.
3- Select a name for the created file. The name selected in this article is LocalDatabaseTutorial.
4- In the Solution pad section, select the LocalDatabasTutorial project and right-click on it.
5- Select Add> Add NuGet Package.
6- In the SQLite-net-PCL window, search Add Packages and add it to the project.
Note:
The specifications of NuGet packages that are suitable for this tutorial and project are as follows:
- Author (s): Frank A. Krueger
- Id: sqlite-net-pcl
- NuGet link: sqlite-net-plc
Build the solution, and then make sure there are no errors.
Learn how to build classes in Xamarin.Form for Mac
1- In the Solution pad, create a new class and name it Person.
2- Replace all the code listed below with the code in the Person. Cs section.
By entering this code, you create and define a class called Person. The ID attribute is defined as the primary key.
3- In the Solution pad of the LocalDatabaseTutorial project, add a Database class to the project.
4- Replace all the code listed below with the code in the Database. Cs.
This class contains code to create and define a database, read data from it, and write data to it.
Database Constructor receives the address of the database file as an argument.
5- In the Solution pad section of the LocalDatabeseTutorial project, select the App. XAML section.
6- Double click on App.XAML.cs.
7- Replace all the codes listed below with those in that section.
By entering this code, you define the Database attribute and create a new instance as a singleton.
The advantage of displaying the Database in singleton is that a database connection is created,. The application is kept open. We do not need to open and close the database file every time we make changes to the Database.
8- Build the solution to make sure there are no errors.
Learn how to use classes to access data in Xamarin.Forms
1- Double-click MainPage.XAML in the Solution pad of the LocalDatabaseTutorial project.
2- Replace the code below with the code in the file.
By entering this code, you specify a UI for the page, which includes two entries, a Button and a ListView, located inside the StackLayout. Each entry has a placeholder attribute.
An event handler is used to press a Button called OnButtonClicked.
3- In the Solution pad section of the LocalDatabaseTutorial project, click on the MainPage.XAML section.
4- Double-click on MainPage.xaml.cs.
5- Then add OnAppearing override and OnButtonClicked event handler to this class.
6- Replace the codes inserted below with the codes in the file.
The OnButton Clicked method works when you hit a button and store the data entered in both entries before clearing and refreshing the data in ListView.
7- Press the start or Ctrl + F5 key combination to see the result of the applied changes.
Enter some data and tap the button to save the data to the Database and display it in ListView.
Finally, you can do the following:
- How to create and add SQLite.NET Use NuGet, Package Manager in Xamarin.Forms projects.
- How to create classes that have access to the data in the Database.
- How to use classes that have access to database data.
DotNek Android development services