【发布时间】:2010-09-06 18:18:30
【问题描述】:
我之前在 C++ 中通过包含 sqlite.h 完成了此操作,但在 C# 中是否有类似的简单方法?
【问题讨论】:
-
这是这个问题的副本:stackoverflow.com/questions/93654/… 并且有不同的答案。
我之前在 C++ 中通过包含 sqlite.h 完成了此操作,但在 C# 中是否有类似的简单方法?
【问题讨论】:
http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 上有一个 .Net 的 Sqlite 包装器列表。据我所知http://sqlite.phxsoftware.com/ 非常好。这个特殊的允许您通过 ADO.Net 访问 Sqlite,就像任何其他数据库一样。
【讨论】:
Microsoft.Data.Sqlite by Microsoft 每天有超过 9000 次下载,所以我认为你可以放心使用它。
来自the documentation的示例用法:
using (var connection = new SqliteConnection("Data Source=hello.db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
SELECT name
FROM user
WHERE id = $id
";
command.Parameters.AddWithValue("$id", id);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var name = reader.GetString(0);
Console.WriteLine($"Hello, {name}!");
}
}
}
【讨论】:
我使用它取得了巨大的成功:
http://system.data.sqlite.org/
免费,没有限制。
(评论注意:原网站已不存在。以上链接有一个指向404网站的链接,并包含原网站的所有信息)
--布鲁斯
【讨论】:
现在还有这个选项:http://code.google.com/p/csharp-sqlite/ - SQLite 到 C# 的完整端口。
【讨论】:
我同意,布鲁斯。我使用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;
}
}
【讨论】:
Mono 带有一个包装器,使用他们的!
https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0 提供代码以以 .net 友好的方式包装实际的 SQLite dll(在下载页面 http://www.sqlite.org/download.html/ 上找到的 http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip)。它适用于 Linux 或 Windows。
这似乎是世界上最薄的,最大限度地减少了您对第三方库的依赖。如果我必须从头开始做这个项目,我会这样做。
【讨论】:
在 NET Framework 中使用 SQLite 数据库的另一种方法是使用 Fluent-NHibernate。
[它是一个 NET 模块,它包裹着 NHibernate(ORM 模块 - 对象关系映射),并允许使用 fluent 模式以编程方式(没有 XML 文件)配置 NHibernate。]
这是关于如何在 C# 中逐步执行此操作的简要“入门”说明:
https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started
它包含作为 Visual Studio 项目的源代码。
【讨论】:
https://github.com/praeclarum/sqlite-net 现在可能是最好的选择。
【讨论】:
如果您对库有任何问题,可以使用Microsoft.Data.Sqlite;
public static DataTable GetData(string connectionString, string query)
{
DataTable dt = new DataTable();
Microsoft.Data.Sqlite.SqliteConnection connection;
Microsoft.Data.Sqlite.SqliteCommand command;
connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source= YOU_PATH_BD.sqlite");
try
{
connection.Open();
command = new Microsoft.Data.Sqlite.SqliteCommand(query, connection);
dt.Load(command.ExecuteReader());
connection.Close();
}
catch
{
}
return dt;
}
你可以添加 NuGet 包 Microsoft.Data.Sqlite
【讨论】:
在这里我试图一步一步地帮助你完成工作:(这可能是其他问题的答案)
差不多就是这样。现在你可以在你的项目中使用 SQLite。 要在代码级别的项目中使用它,您可以使用以下示例代码:
创建一个连接字符串:
string connectionString = @"URI=file:{the location of your sqlite database}";
建立一个sqlite连接:
SQLiteConnection theConnection = new SQLiteConnection(connectionString );
打开连接:
theConnection.Open();
创建一个 sqlite 命令:
SQLiteCommand cmd = new SQLiteCommand(theConnection);
制作一个命令文本,或者更好地说你的 SQLite 语句:
cmd.CommandText = "INSERT INTO table_name(col1, col2) VALUES(val1, val2)";
执行命令
cmd.ExecuteNonQuery();
就是这样。
【讨论】: