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");
            }
        }
    }
}

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

enter image description here

我错过了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...