What is the best way to connect and use a sqlite database from C#

前端 未结 9 898
执笔经年
执笔经年 2020-11-29 19:24

I\'ve done this before in C++ by including sqlite.h but is there a similarly easy way in C#?

相关标签:
9条回答
  • 2020-11-29 19:48

    https://github.com/praeclarum/sqlite-net is now probably the best option.

    0 讨论(0)
  • 2020-11-29 19:49

    if you have any problem with the library you can use 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;
            }
    

    you can add NuGet Package Microsoft.Data.Sqlite

    0 讨论(0)
  • 2020-11-29 19:50

    I'm with, Bruce. I AM using http://system.data.sqlite.org/ with great success as well. Here's a simple class example that I created:

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

    There is also an NuGet package: System.Data.SQLite available.

    0 讨论(0)
  • 2020-11-29 19:58

    Mono comes with a wrapper, use theirs!

    https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0 gives code to wrap the actual SQLite dll ( http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip found on the download page http://www.sqlite.org/download.html/ ) in a .net friendly way. It works on Linux or Windows.

    This seems the thinnest of all worlds, minimizing your dependence on third party libraries. If I had to do this project from scratch, this is the way I would do it.

    0 讨论(0)
  • 2020-11-29 20:02

    Another way of using SQLite database in NET Framework is to use Fluent-NHibernate.
    [It is NET module which wraps around NHibernate (ORM module - Object Relational Mapping) and allows to configure NHibernate programmatically (without XML files) with the fluent pattern.]

    Here is the brief 'Getting started' description how to do this in C# step by step:

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

    It includes a source code as an Visual Studio project.

    0 讨论(0)
  • 2020-11-29 20:04

    I've used this with great success:

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

    Free with no restrictions.

    (Note from review: Original site no longer exists. The above link has a link pointing the the 404 site and has all the info of the original)

    --Bruce

    0 讨论(0)
提交回复
热议问题