1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data.Linq.Mapping;
using DbLinq.Data.Linq;


namespace LinqToSQLite
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "DbLinqProvider=Sqlite;Data Source=db2.sqlite";

            DataContext db = new DataContext(connectionString);

            db.ExecuteCommand(Person.CreateCommand);
            Table<Person> table = db.GetTable<Person>();

            table.InsertOnSubmit(new Person { Name = "Alfred" });
            table.InsertOnSubmit(new Person { Name = "Brian" });
            table.InsertOnSubmit(new Person { Name = "Charles" });

            db.SubmitChanges();

            var query = from p in table select p;
            foreach (var p in query)
            {
                Console.WriteLine(p.ID + ". " + p.Name);
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }
    }

    [Table(Name = "Persons")]
    class Person
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID { get; set; }

        [Column]
        public string Name { get; set; }

        public static string CreateCommand = "CREATE TABLE IF NOT EXISTS Persons ( Id INTEGER PRIMARY KEY, Name TEXT )";
    }
}

我创建了简单的应用程序,使用 dblinq 向 SQLite 提供 Linq,但它挂起db.SubmitChanges()。什么原因?我只是想在不使用 DbMetal.exe 的情况下创建简单的 dblinq 应用程序......没有错误,没有例外我一无所知。

4

1 回答 1

1

问题出在 DataContext 和 connectonString 上,要修复它我必须更改...

string connectionString = "DbLinqProvider=Sqlite;Data Source=db2.sqlite;";
SQLiteConnection connection = new SQLiteConnection(connectionString);

DataContext db = new DataContext(connection);

...或将DbLinqConnectionType添加到 connectionString ...

string connectionString = "DbLinqProvider=Sqlite;Data Source=db2.sqlite;";
connectionString += "DbLinqConnectionType=System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139";

DataContext db = new DataContext(connectionString);
于 2012-12-11T07:07:19.283 回答