从C#连接和使用sqlite数据库的最佳方法是什么

时间:2020-03-05 18:42:48  来源:igfitidea点击:

我之前在C ++中通过包含sqlite.h做到了这一点,但是C#中是否有类似的简单方法?

解决方案

回答

在http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers上有一个用于.Net的Sqlite包装器列表。从我听说的http://sqlite.phxsoftware.com/来看,它是相当不错的。这一特定功能使我们可以像其他任何数据库一样通过ADO.Net访问Sqlite。

回答

用于SQLite的ADO.NET 2.0 Provider每天有200多次下载,因此我认为使用该版本是安全的。

回答

我已经成功地使用了它:

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;
  }
}

还有一个NuGet包:System.Data.SQLite可用。