题
我之前在 C++ 中通过包含 sqlite.h 完成了此操作,但是在 C# 中是否有类似的简单方法?
解决方案
SQLite 的 ADO.NET 2.0 提供程序 每天有超过 200 次下载,所以我认为您使用它是安全的。
其他提示
我和布鲁斯在一起。我在用 http://system.data.sqlite.org/ 也取得了巨大的成功。这是我创建的一个简单的类示例:
using System;
using System.Text;
using System.Data;
using System.Data.SQLite;
namespace MySqlLite
{
class DataClass
{
private SQLiteConnection sqlite;
public DataClass()
{
//This part killed me in the beginning. I was specifying "DataSource"
//instead of "Data Source"
sqlite = new SQLiteConnection("Data Source=/path/to/file.db");
}
public DataTable selectQuery(string query)
{
SQLiteDataAdapter ad;
DataTable dt = new DataTable();
try
{
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
cmd.CommandText = query; //set the passed query
ad = new SQLiteDataAdapter(cmd);
ad.Fill(dt); //fill the datasource
}
catch(SQLiteException ex)
{
//Add your exception code here.
}
sqlite.Close();
return dt;
}
}
还有一个 NuGet 包:系统.数据.SQLite 可用的。
我用这个取得了巨大的成功:
http://system.data.sqlite.org/
免费,没有任何限制。
(评论注:原来的网站已经不存在了。上面的链接有一个指向 404 站点的链接,并且包含原始的所有信息)
——布鲁斯
.Net 的 Sqlite 包装器列表位于 http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers. 。据我所知 http://sqlite.phxsoftware.com/ 相当不错。这个特殊的数据库允许您通过 ADO.Net 访问 Sqlite,就像任何其他数据库一样。
现在还有这个选项: http://code.google.com/p/csharp-sqlite/ - SQLite 到 C# 的完整移植。
https://github.com/praeclarum/sqlite-net 现在可能是最好的选择。
在 NET Framework 中使用 SQLite 数据库的另一种方法是使用 Fluent-NHibernate.
[它是包裹 NHibernate(ORM 模块 - 对象关系映射)的 NET 模块,并允许使用流畅的模式以编程方式(没有 XML 文件)配置 NHibernate。]
以下是如何在 C# 中逐步执行此操作的简短“入门”描述:
https://github.com/jagregory/ Fluent-nhibernate/wiki/Getting-started
它包括作为 Visual Studio 项目的源代码。
Mono 带有一个包装纸,使用他们的!
https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0 给出了包装实际 SQLite dll 的代码( http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip 在下载页面找到 http://www.sqlite.org/download.html/ )以 .net 友好的方式。它可以在 Linux 或 Windows 上运行。
这似乎是所有世界中最薄的,最大限度地减少了您对第三方库的依赖。如果我必须从头开始做这个项目,我就会这样做。