【问题标题】:What is the best way to connect and use a sqlite database from C#从 C# 连接和使用 sqlite 数据库的最佳方法是什么
【发布时间】:2010-09-06 18:18:30
【问题描述】:

我之前在 C++ 中通过包含 sqlite.h 完成了此操作,但在 C# 中是否有类似的简单方法?

【问题讨论】:

标签: c# sqlite


【解决方案1】:

http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 上有一个 .Net 的 Sqlite 包装器列表。据我所知http://sqlite.phxsoftware.com/ 非常好。这个特殊的允许您通过 ADO.Net 访问 Sqlite,就像任何其他数据库一样。

【讨论】:

    【解决方案2】:

    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}!");
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我使用它取得了巨大的成功:

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

      免费,没有限制。

      (评论注意:原网站已不存在。以上链接有一个指向404网站的链接,并包含原网站的所有信息)

      --布鲁斯

      【讨论】:

        【解决方案4】:

        现在还有这个选项:http://code.google.com/p/csharp-sqlite/ - SQLite 到 C# 的完整端口。

        【讨论】:

          【解决方案5】:

          我同意,布鲁斯。我使用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 可用。

          【讨论】:

          • "数据源=/path/to/file.db;New=False;" 如果您不想每次都丢失所有数据和表格
          • 它在 VS 2017 中不起作用。它显示以下错误:未处理的异常:System.BadImageFormatException:无法加载文件或程序集 'System.Data.SQLite,版本 = 1.0.79.0,文化 = 中性, PublicKeyToken=db937bc2d44ff139'。试图加载格式不正确的程序。在 ConsoleApp1_del2.Program.Main(String[] args)
          【解决方案6】:

          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。

          这似乎是世界上最薄的,最大限度地减少了您对第三方库的依赖。如果我必须从头开始做这个项目,我会这样做。

          【讨论】:

            【解决方案7】:

            在 NET Framework 中使用 SQLite 数据库的另一种方法是使用 Fluent-NHibernate
            [它是一个 NET 模块,它包裹着 NHibernate(ORM 模块 - 对象关系映射),并允许使用 fluent 模式以编程方式(没有 XML 文件)配置 NHibernate。]

            这是关于如何在 C# 中逐步执行此操作的简要“入门”说明:

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

            它包含作为 Visual Studio 项目的源代码。

            【讨论】:

              【解决方案8】:

              https://github.com/praeclarum/sqlite-net 现在可能是最好的选择。

              【讨论】:

                【解决方案9】:

                如果您对库有任何问题,可以使用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

                【讨论】:

                  【解决方案10】:

                  在这里我试图一步一步地帮助你完成工作:(这可能是其他问题的答案)

                  1. 转到this address,在页面下方您可以看到类似“发行包列表”的内容。根据您的系统和 .net 框架版本选择适合您的版本。例如,如果您想在 64 位 Windows 上使用 .NET Framework 4.6,请选择 this version 并下载它。
                  2. 然后将文件安装在硬盘上的某个位置,就像安装任何其他软件一样。
                  3. 打开 Visual Studio 和您的项目。然后在解决方案资源管理器中,右键单击“References”并选择“add Reference...”。
                  4. 单击浏览按钮并选择安装上一个文件的位置,然后转到 .../bin/System.Data.SQLite.dll 并单击添加,然后单击确定按钮。

                  差不多就是这样。现在你可以在你的项目中使用 SQLite。 要在代码级别的项目中使用它,您可以使用以下示例代码:

                  1. 创建一个连接字符串:

                    string connectionString = @"URI=file:{the location of your sqlite database}";

                  2. 建立一个sqlite连接:

                    SQLiteConnection theConnection = new SQLiteConnection(connectionString );

                  3. 打开连接:

                    theConnection.Open();

                  4. 创建一个 sqlite 命令:

                    SQLiteCommand cmd = new SQLiteCommand(theConnection);

                  5. 制作一个命令文本,或者更好地说你的 SQLite 语句:

                    cmd.CommandText = "INSERT INTO table_name(col1, col2) VALUES(val1, val2)";

                  6. 执行命令

                    cmd.ExecuteNonQuery();

                  就是这样。

                  【讨论】:

                    猜你喜欢
                    • 2011-03-20
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-03-19
                    • 2010-09-24
                    • 1970-01-01
                    相关资源
                    最近更新 更多