2008-08-25 97 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项目。

相关问题