I want to create a DB for SQLite for use in the store application in c#

Asked 2 years ago, Updated 2 years ago, 84 views

Nice to meet you.
I received a lot of comments from the previous question and even set up settings to use SQLite.
However, I tried to find out how to create a DB to see if it really touched SQLite, but I could only find a way to add System.Data.SClite.dll.

Install SQLite for Windows Runtime and
Add SQLite for Windows Runtime and c++ 2013 to
Installing sqlite-net is over.

I made a button with xaml.Some statements begin with a private void that writes click event processing to MainPage.xaml.cs.

private void connect_bt(object sender, RoutedEventArgse)
    {

    }

Do you write a description of connecting to or creating a DB through SQLite?
I can't find any information and I can't check if the DB connection works.

What is the description of using SClite in c# to create a DB for use in the store application?

I don't mind if it's a reference site, so please let me know.
Thank you for your cooperation.

c# visual-studio sqlite database

2022-09-30 11:30

1 Answers

Now that we have implemented it, we would like to place the source code here.

public sold partial class MainPage:Page
{
    publicMainPage()
    {
        This.InitializeComponent();
        Loaded + = MainPage_Loaded;
    }

    voidMainPage_Loaded(object sender, RoutedEventArgse)
    {
        // decide where to save the database and the name of the database file
        vardbpath=Path.Combine (Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Food.db");
        // Connect to the database file specified in dbpath
        using(vardb=newSQLite.SQLiteConnection(dbpath))
        {
            // Create Table
            db.CreateTable<Food>();
            // transaction processing
            db.RunInTransaction()=>
            {
                // Creating a Record
                db.Insert(new Food() {Name="Shoyu Ramen", Price=500, Calorie=700});
                db.Insert(new Food() {Name="Kitsune Udon", Price=350, Calorie=500});
                db.Insert(new Food() {Name="Curry Rice", Price=450, Calorie=1000});
                db.Insert(new Food() {Name="Potato Chips", Price=200, Calorie=600});
            });

            // Set to ListView Source
            Food_listview.ItemsSource=db.Table<Food>();
        }
    }

}

public class food
{
    // main key, automatic serial number
    AutoIncrement, PrimaryKey
    public int Id {get;set;}

    // Column Type and Name
    public string name {get;set;}
    public int Price {get;set;}
    public int Calorie {get;set;}

    // output format
    public override string ToString()
    {
        return string.Format("ID: {0}\tProduct Name: {1}\tPrice: {2} yen\tCalorie: {3}kcal", Id, Name, Price, Calorie);
    }
}

Thank you to everyone who commented.


2022-09-30 11:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.