本人还是挺喜欢用Sqlite,鼓捣半天终于连上了,赶紧记录一下
1.当然还是新建一个项目,还是winform,
2.Vs2012添加NoGet,点击工具--扩展和更新,搜索NoGet,安装。
3.管理NoGet程序包,从工具中选,或右键点击项目都可,搜索Sqlite,安装
4.重新编译一下程序,添加SqliteDataContext类,代码如下
/// <summary>
/// 在“管理NUGET程序包”安装LINQ程序包
/// 然后使用DataContext就可以连接sqlite了
/// 引用要添加 system.Data.Linq;system.Data.SQLite;system.Data.SQLite.Linq
public class SqliteDataContext : DataContext
{
public SqliteDataContext(string connection, MappingSource mappingSource) :
base(connection, mappingSource)
{
}
public SqliteDataContext(IDbConnection connection, MappingSource mappingSource) :
base(connection, mappingSource)
{
}
public SqliteDataContext(string connectionString) :
base(new SQLiteConnection(connectionString))
{
}
public SqliteDataContext(IDbConnection connection) :
base(connection)
{
} public Table<Article> Article
{
get
{
return this.GetTable<Article>();
}
}
}
5.调用方式
SqliteDataContext db = new SqliteDataContext(@"data source=D:\Documents\Visual Studio 2012\Projects\Yss20140406\Yss20140406\bin\Debug\YssDB");
var temp =
from t in db.Article
select t;
dataGridView1.DataSource = temp;
6.如果直接写上面的代码是会报错的,因为没有写实体类啊,可以自己写。我是用DbLinq生成的,只复制部分代码。如果完全用DbLinq生成的类会报错,与上面的引用有冲突,所以复制粘贴一下好了。
[Table(Name = "main.Article")]
public partial class Article
{ private string _context; private string _title; #region Extensibility Method Declarations
partial void OnCreated(); partial void OnContextChanged(); partial void OnContextChanging(string value); partial void OnTitleChanged(); partial void OnTitleChanging(string value);
#endregion public Article()
{
this.OnCreated();
} [Column(Storage = "_context", Name = "Context", DbType = "text", AutoSync = AutoSync.Never, CanBeNull = false)]
public string Context
{
get
{
return this._context;
}
set
{
if (((_context == value)
== false))
{
this.OnContextChanging(value);
this._context = value;
this.OnContextChanged();
}
}
} [Column(Storage = "_title", Name = "Title", DbType = "text", AutoSync = AutoSync.Never, CanBeNull = false)]
public string Title
{
get
{
return this._title;
}
set
{
if (((_title == value)
== false))
{
this.OnTitleChanging(value);
this._title = value;
this.OnTitleChanged();
}
}
}
}
附1:在Vs2012中操作Sqlite,安装下sqlite-netFx45-setup-bundle-x86-2012-1.0.84.0就可以了,官网有下。
附2:Dblinq网上有很多使用方法,我就不详细写了,我的下载包里也有,包含命令。
附3:练习代码下载