Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sqlite

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

like image 465
Richard Gourlay Avatar asked Aug 25 '08 13:08

Richard Gourlay


People also ask

How do I connect to a SQLite database?

To establish a database connection to an SQLite database, you need to create a new instance of the PDO class and pass a connection string to the constructor of the PDO object. Because you store the path to the sqlite database file in the Config class, you just simply use it to construct the connection string.

What is SQLite c#?

SQLite is an embedded relational database engine. It is a self-contained, serverless, zero-configuration and transactional SQL database engine. SQLite implements most of the SQL-92 standard for SQL. The SQLite engine is not a standalone process. Instead, it is statically or dynamically linked into the application.

How do I connect SQLite to a Windows application?

Open your Visual Studio and select new project and in Visual C# select "Windows Forms Application" and provide the name as Sqlite and click on OK. Right-click on your application and select "Open folder in your window application" and then go to: BIN -> Debug and extract your application here.

Why you should not use SQLite?

High write volumes: SQLite allows only one write operation to take place at any given time, which significantly limits its throughput. If your application requires lots of write operations or multiple concurrent writers, SQLite may not be adequate for your needs.


1 Answers

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.

like image 187
DA. Avatar answered Sep 20 '22 16:09

DA.