[英]What is the best way to connect and use a sqlite database from C#
我之前通過包含 sqlite.h 在 C++ 中完成了此操作,但是在 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;
}
}
還有一個NuGet 包:System.Data.SQLite可用。
Microsoft.Data.Sqlite每天有超過 9000 次下載,所以我認為你可以安全地使用它。
文檔中的示例用法:
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://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 上有一個 .Net 的 Sqlite 包裝器列表。 據我所知, 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 的 NET 模塊(ORM 模塊 - 對象關系映射),並允許使用 fluent 模式以編程方式(沒有 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.dll )。 org/sqlite-shell-win32-x86-3071300.zip在下載頁面http://www.sqlite.org/download.html/上以 .net 友好的方式找到。 它適用於 Linux 或 Windows。
這似乎是所有世界中最薄的,最大限度地減少了對第三方庫的依賴。 如果我必須從頭開始做這個項目,我會這樣做。
在這里,我試圖幫助您一步一步地完成工作:(這可能是其他問題的答案)
差不多就是這樣。 現在您可以在您的項目中使用 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();
這就對了。
如果您對庫有任何問題,可以使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.