问题描述
我正在尝试了解 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");
}
}
}
}
我错过了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)