Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

UWP: insert portable SQLite database

29.06.2018 (👁16153)


 

 

Under UWP, it sometimes makes sense to create a local database for the application.

It is important that the database is easily created on-the-fly at runtime.

That's why the Sqlite database is best suited here.

This has also been equipped with EF Entity Framework and Linq since 2018.

 

Note: Since the Windows 10 Anniversary Update (build 14393), SQLite is part of Windows 10.

This means that you only have to use Microsoft.data.SQLite as the reference

 

Installation in UWP App

 

In Visual Studio, select the project and in the context click on -> Manage NuGet Packages.

 

In the NuGet Package Manager, enter: sqlite in Browse and select the package

Microsoft.Data.Sqlite from Microsoft

 

SQLite implementation of the System.Data.Common provider model.

 

Commonly Used Types:

Microsoft.Data.Sqlite.SqliteCommand

Microsoft.Data.Sqlite.SqliteConnection

Microsoft.Data.Sqlite.SqliteConnectionStringBuilder

Microsoft.Data.Sqlite.SqliteDataReader

Microsoft.Data.Sqlite.SqliteException

Microsoft.Data.Sqlite.SqliteFactory

Microsoft.Data.Sqlite.SqliteParameter

Microsoft.Data.Sqlite.SqliteTransaction

 

 

Include in C # Code

In the UWP page MainPage.cs enter the namespace Microsoft.Data.SQlite in the Using header

 

 

//< using >

using HtmlAgilityPack;          //HtmlDocument

using System.Net;               //*WebRequest

using System.Threading.Tasks;   //*Task

using Microsoft.Data.Sqlite;    //*SQLite for UWP from Microsoft

//</ using >

 

 

Subsequently, the database can be created at runtime while the main page is opened

public MainPage()

{

    this.InitializeComponent();

    optStop.IsChecked = false;

 

    //SqliteEngine.UseWinSqlite3(); //Configuring library to use SDK version of SQLite

    using (SqliteConnection db = new SqliteConnection("Filename=Projects.db"))

    {

        db.Open();

        String tableCommand = "CREATE TABLE IF NOT EXISTS MyTable (Primary_Key INTEGER PRIMARY KEY AUTOINCREMENT, Text_Entry NVARCHAR(2048) NULL)";

        SqliteCommand createTable = new SqliteCommand(tableCommand, db);

        try

        {

            createTable.ExecuteReader();

        }

        catch (SqliteException e)

        {

            //Do nothing

        }

    }

}

 

 

 

 

Read SQLite data

Reading and writing data in the SQLite database is pretty easy

https://codedocu.de/Net-Framework/UWP-Universal-Windows-Plattform/Datenbank/SQLite_colon_-komplette-SQL-Datenbank-in-Universal-Windows-App-als-Beispiel-SQLitePCL?1682

 

 

SQLiteConnection dbConnection = new SQLiteConnection("Folders.db"); 
string sSQL = @"CREATE TABLE IF NOT EXISTS Folders 
(IDFolder Integer Primary Key Autoincrement NOT NULL 
, Foldername VARCHAR(200) 
, Path VARCHAR(255));"; 
ISQLiteStatement cnStatement = dbConnection.Prepare(sSQL); 
cnStatement.Step(); 

 

 

 

For data processing one finds then https://blogs.windows.com/buildingapps/2017/02/06/using-sqlite-databases-uwp-apps/#k24zYWkVYzaczTzM.97