【问题标题】:Creating database using DataContext and System.Data.SQLite in C#在 C# 中使用 DataContext 和 System.Data.SQLite 创建数据库
【发布时间】:2012-03-17 06:06:21
【问题描述】:

我正在编写简单的应用程序来收集有关机器硬件的信息。我计划将数据存储在 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();            
        }
    }
}

运行这个程序会产生异常:

Unhandled Exception: System.Data.SQLite.SQLiteException: SQLite error
near "DATABASE": syntax error
   at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQ
LiteStatement previous, UInt32 timeoutMS, String& strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavi
or behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteCommand(String command)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider
.CreateDatabase()
   at System.Data.Linq.DataContext.CreateDatabase()
   at SQLiteTest.Program.Main(String[] args) in C:\Users\bootch\documents\visual
 studio 2010\Projects\SQLiteTest\SQLiteTest\Program.cs:line 47

1) 为什么这段代码不起作用,我错过了什么?

2) 有没有办法获取底层 sql 命令的文本版本以查看问题所在

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

提前感谢 cmets 和答案!

【问题讨论】:

    标签: c# linq system.data.sqlite


    【解决方案1】:

    看起来您尝试将 Linq2Sql 与默认情况下不支持的 Sqlite 一起使用(仅支持 SQL Server 和 SQL Server CE),请参阅this question 了解更多详细信息。

    看看使用Entity Framework,它支持Sqlite,而不是。

    【讨论】:

      【解决方案2】:

      我刚刚经历了这个并尝试实施。得到同样的错误,然后发现我们可以使用SQLiteConnection创建数据库,如下所示:

      sqlcnn.CreateFile("MyDatabase.sqlite");
      

      然后使用“db”就可以进行所有的数据库操作了。试试看。

      【讨论】:

        【解决方案3】:

        我发现这对我有用。

                    string fileDB = "Data Source=:memory:";
                    Console.WriteLine(fileDB);
                    SQLiteConnection conn = new SQLiteConnection(fileDB);
                    DataContext db = new DataContext(conn);
                    Console.WriteLine(db.Connection.ServerVersion);
        

        使用文件,将文件路径归功于this guy

        string fileDB = "Data Source=" + Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName +
                                    @"\test.sqlite";
        

        首先查看Linq2SQLhere的公司示例。

        【讨论】:

          猜你喜欢
          • 2021-05-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-25
          • 2010-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多