C# SQLite3 数据库基本查询

我正在尝试了解 C# 以及与 SQLite3 的联系,最好的学习方法是在 Visual Studio 中使用 C# 代码编写一些简单的 SQL 查询。

我使用 sqlite-net-pcl 包来做这个。

我学习了一些教程,但它们有时很难理解,所以我试图从基础开始。

首先,我正在创建 SQLite 类并使用属性。它只会有一个名字和姓氏:

namespace SebSQLiteTest.Classes
{
    class Contact
    {
        [PrimaryKey,AutoIncrement]
        public int ID { get; set; }
        public string Name  { get; set; }
        public string Surname { get; set; }
        
    }
}

然后,我创建一些 SQLite 数据库字符串,然后在桌面上创建一个数据库文件,然后在数据库中创建一个表:

using System;
using SebSQLiteTest.Classes;
using sqliteconn = SQLite.SQLiteConnection;  // creating an alias

namespace SebSQLiteTest
{
    class Program
    {
        static string databaseName = "SebContacts.db";
        static string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        public static string databasePath = System.IO.Path.Combine(folderPath,databaseName);

        static void Main(string[] args)
        {
            Createtable();

            AddContact();
        }

        private static void AddContact()
        {
            Contact contact = new Contact()
            {
                Name = "Sebastian",Surname = "Johnson"
            };

            using(sqliteconn conn = new sqliteconn(databasePath))
            {
                conn.Createtable<Contact>();
                conn.Insert(contact);
                Console.WriteLine($"{contact.Name} {contact.Surname} Added to the database"); 
                // nothing added to the database
            }
        }

        private static void Createtable()
        {
            using (sqliteconn conn = new sqliteconn(databasePath))
            {
                conn.Createtable<Contact>();
                Console.WriteLine("Table Created");
            }
        }
    }
}

程序没有抛出任何错误,但没有向这些表中添加任何内容:

C# SQLite3 数据库基本查询

我错过了什么?

alno_wang 回答:C# SQLite3 数据库基本查询

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/157.html

大家都在问