2008-08-25 95 views
37

我已經在C++中通過包含sqlite.h來完成此操作,但在C#中有類似的簡單方法嗎?連接和使用C#中的sqlite數據庫的最佳方式是什麼?

+3

這是這個問題的重複:http://stackoverflow.com/questions/93654/is-there-a-netc-wrapper-for-sqlite並有不同的答案。 – 2008-09-24 14:47:20

+0

可能的重複[是否有SQLite的.NET/C#包裝?](https://stackoverflow.com/questions/93654/is-there-a-net-c-wrapper-for-sqlite) – Flimzy 2017-07-17 09:26:05

回答

12

我已經取得了巨大成功使用此:

http://system.data.sqlite.org/

免費,沒有任何限制。

(從審覈注:原站點不再存在上面的鏈接有指向404網站的鏈接,並具有原始的所有信息。)

--Bruce

60

我與布魯斯。我使用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 package: System.Data.SQLite可用。

1

單聲道帶有包裝,使用它們!

https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0給出的代碼以.net友好的方式包裝實際的SQLite dll(http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip在下載頁http://www.sqlite.org/download.html/上找到)。它適用於Linux或Windows。

這似乎是所有世界中最薄的一種,最大限度地減少了對第三方庫的依賴。如果我必須從頭開始這個項目,這是我會這樣做的方式。

3

在.NET Framework中使用SQLite數據庫的另一種方法是使用Fluent-NHibernate
[這是一個包含NHibernate(ORM模塊 - 對象關係映射)的NET模塊,並允許以流暢模式編程(無XML文件)配置NHibernate。]

這是「入門」一步地做到這一點在C#中的步驟:

https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started

它包括源代碼作爲Visual Studio項目。

相關問題