有人可以发布一个使用 DBLinq、SQLite 进行工作连接的小代码示例吗?我已经在 VS 2010 WPF 环境中努力让它启动并运行两天了。我想我已经解决了连接字符串,但很想看到示例的启动和运行。
var con = new SQLiteConnection("DbLinqProvider=Sqlite;Version=3;Data Source=c:\\temp\\testdb.db3;");
DataSource db = new DataSource(con);
var q = from c in db.Person
select c;
foreach (Person tempPerson1 in q)
MessageBox.Show(tempPerson1.Name);
我的DBML文件(相关代码) - 我将“Main”更改为“DataSource”,将 SQLite 更改为 System.Data.SQLite.SQLiteConnection 以对其进行编译。
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="DataSource")]
[global::System.Data.Linq.Mapping.ProviderAttribute(typeof(System.Data.SQLite.SQLiteConnection))]
public DbLinq.Data.Linq.Table<Person> Person {
get {
return this.GetTable<Person>();
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="Datasource.Person")]
public partial class Person {
private string _id;
private string _name;
public Person() { }
[global::System.Data.Linq.Mapping.ColumnAttribute(
Name="id", Storage="_id", DbType="VARCHAR(10)")]
public string ID {
get {
return this._id;
}
set {
if ((this._id != value)) {
this._id = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(
Name="name", Storage="_name", DbType="VARCHAR(25)")]
public string Name {
get {
return this._name;
}
set {
if ((this._name != value)) {
this._name = value;
}
}
}
}
我目前收到一个 SQLite 错误,表明没有这样的表:Datasource.Person,我相信我的路径和连接字符串正确。我应该从 DBMetal 生成 DBML 文件和 CS 文件吗?
最佳答案
解决方案:我重新生成了 DBML 文件,没有将“main”更改为其他名称,包括对“Using System.Data.SQLite”的引用并进行了更改
[global::System.Data.Linq.Mapping.ProviderAttribute(typeof(Sqlite))]
至
[global::System.Data.Linq.Mapping.ProviderAttribute(typeof(SQLiteConnection))]
似乎现在正在工作,我终于从我的数据库中得到了结果。
关于sqlite - 使用 DBLinq、SQLite 的代码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6416373/