从 C# 连接和使用 sqlite 数据库的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to connect and use a sqlite database from C#
提问by Richard Gourlay
I've done this before in C++ by including sqlite.h but is there a similarly easy way in C#?
我之前通过包含 sqlite.h 在 C++ 中完成了此操作,但是在 C# 中是否有类似的简单方法?
采纳答案by Espo
ADO.NET 2.0 Provider for SQLitehas over 200 downloads every day, so I think you are safe using that one.
ADO.NET 2.0 Provider for SQLite每天有超过 200 次下载,所以我认为你可以安全地使用它。
回答by robintw
There is a list of Sqlite wrappers for .Net at http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers. From what I've heard http://sqlite.phxsoftware.com/is quite good. This particular one lets you access Sqlite through ADO.Net just like any other database.
在http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 上有一个 .Net 的 Sqlite 包装器列表。据我所知,http://sqlite.phxsoftware.com/ 相当不错。这个特殊的允许您像任何其他数据库一样通过 ADO.Net 访问 Sqlite。
回答by bvanderw
I've used this with great success:
我已经成功地使用了它:
http://system.data.sqlite.org/
http://system.data.sqlite.org/
Free with no restrictions.
免费,没有任何限制。
(Note from review: Original site no longer exists. The above link has a link pointing the the 404 site and has all the info of the original)
(评论注意:原站点已不存在。上面的链接有一个指向 404 站点的链接,并包含原始站点的所有信息)
--Bruce
——布鲁斯
回答by xanadont
There's also now this option: http://code.google.com/p/csharp-sqlite/- a complete port of SQLite to C#.
现在还有这个选项:http: //code.google.com/p/csharp-sqlite/- SQLite 到 C# 的完整端口。
回答by DA.
I'm with, Bruce. I AM using http://system.data.sqlite.org/with great success as well. Here's a simple class example that I created:
我和布鲁斯在一起。我也在使用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;
}
}
There is also an NuGet package: System.Data.SQLiteavailable.
还有一个NuGet 包:System.Data.SQLite可用。
回答by Limited Atonement
Mono comes with a wrapper, use theirs!
Mono 带有包装器,请使用他们的包装器!
https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0gives code to wrap the actual SQLite dll ( http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zipfound on the download page http://www.sqlite.org/download.html/) in a .net friendly way. It works on Linux or Windows.
https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0提供了包装实际 SQLite dll 的代码(http://www.sqlite.dll )。 org/sqlite-shell-win32-x86-3071300.zip在下载页面http://www.sqlite.org/download.html/上找到)以一种 .net 友好的方式。它适用于 Linux 或 Windows。
This seems the thinnest of all worlds, minimizing your dependence on third party libraries. If I had to do this project from scratch, this is the way I would do it.
这似乎是所有世界中最薄的,最大限度地减少了对第三方库的依赖。如果我必须从头开始做这个项目,我会这样做。
回答by Bronek
Another way of using SQLite database in NET Framework is to use Fluent-NHibernate.
[It is NET module which wraps around NHibernate (ORM module - Object Relational Mapping) and allows to configure NHibernate programmatically (without XML files) with the fluent pattern.]
在 NET Framework 中使用 SQLite 数据库的另一种方法是使用Fluent-NHibernate。
[它是包装 NHibernate 的 NET 模块(ORM 模块 - 对象关系映射),并允许使用 fluent 模式以编程方式(没有 XML 文件)配置 NHibernate。]
Here is the brief 'Getting started' description how to do this in C# step by step:
以下是如何在 C# 中逐步执行此操作的简短“入门”说明:
https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started
https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started
It includes a source code as an Visual Studio project.
它包含作为 Visual Studio 项目的源代码。
回答by xanadont
https://github.com/praeclarum/sqlite-netis now probably the best option.
https://github.com/praeclarum/sqlite-net现在可能是最好的选择。