C# 在C中使用DataContext和System.Data.SQLite创建数据库#

C# 在C中使用DataContext和System.Data.SQLite创建数据库#,c#,linq,system.data.sqlite,C#,Linq,System.data.sqlite,我正在编写一个简单的应用程序,收集有关机器硬件的信息。我计划在sqlite中存储数据。获得以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; using System.IO; using System.Data; using System.Data.Linq; using System.Data.Linq.Mapp

我正在编写一个简单的应用程序,收集有关机器硬件的信息。我计划在sqlite中存储数据。获得以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.IO;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace SQLiteTest
{
    public class MyDB : DataContext
    {
        public Table<Computer> kompy;
        public MyDB(string connection) : base(connection) { }
        public MyDB(IDbConnection connection) : base(connection) { }
    }


    [Table]
    public class Computer
    {
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public uint CompId;

        [Column]
        public uint CompanyId;

        [Column]
        public string CompName;
    }


    class Program
    {
        static void Main(string[] args)
        {
            string rootPath = Environment.CurrentDirectory;
            string dbPath = rootPath + "\\db.sqlite3";

            SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
            builder.Add("Data Source", dbPath);

            SQLiteConnection sqlcnn = new SQLiteConnection(builder.ConnectionString);
            MyDB db = new MyDB(sqlcnn);
            db.CreateDatabase();
            db.SubmitChanges();
            db.Dispose();            
        }
    }
}
1) 为什么这个代码不起作用,我遗漏了什么

2) 有没有一种方法可以获取底层sql命令的文本版本,以查看出了什么问题

3) 若数据库不存在,我希望创建数据库和所有表。我很懒,所以我想我可以使用我创建的类型来运行查询。是否可以使用System.Data.SQLite


提前感谢您的评论和回答

看起来您正在尝试将Linq2Sql与Sqlite一起使用,默认情况下不支持该功能(仅支持SQL Server和SQL Server CE),有关详细信息,请参阅


看看using,它支持Sqlite。

我刚刚讨论了这个问题,并尝试实现它。得到相同的错误,然后发现我们可以使用
SQLiteConnection
创建数据库,如下所示:

sqlcnn.CreateFile("MyDatabase.sqlite");
然后使用“db”可以执行所有数据库操作。试试看

sqlcnn.CreateFile("MyDatabase.sqlite");