How to send data from Exele file to UWP SQLite after Generate an UWP package

52 views Asked by At

I am using Microsoft Data SQLite NuGet package inside UWP App,

I have 300 record in Exele sheet after creating UWP app package I want to send these 300 record to my SQLite database,how can I do that I even not know where is the database file and how to open it and how do I send these record

1

There are 1 answers

1
Junjie Zhu - MSFT On

You can refer to this official document Use a SQLite database in a UWP app.

There is a small bug in the docs, You need to install Microsoft.Data.SQLite instead of Microsoft.Data.SQLite.Core.

Create a SQLite database in LocalFolder with ApplicationData.Current.LocalFolder, the .db file will be created in your local folder: C:\Users\YourUserName\AppData\Local\Packages\9be...(Your App Package FamilyName)\LocalState\sqliteSample.db

ApplicationData.Current.LocalFolder.CreateFileAsync("sqliteSample.db", CreationCollisionOption.OpenIfExists);

You can get the database through Path, and add your excel data with SqliteCommand.

string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
using (SqliteConnection db =
    new SqliteConnection($"Filename={dbpath}"))
    {
        db.Open();
        SqliteCommand insertCommand = new SqliteCommand();
        insertCommand.Connection = db;

        // Use parameterized query to prevent SQL injection attacks
        insertCommand.CommandText = "INSERT INTO MyTable VALUES (NULL, @Entry);";
        insertCommand.Parameters.AddWithValue("@Entry", inputText);

        insertCommand.ExecuteReader();
    }