How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

How to create and connect to an SQLite database in C#

The aim of this tutorial is to teach you how to create a new SQLite database from scratch, create a new table in it, insert and read values from it. This is merely an entry level example to give you an idea on how to start.

First you will need System.Data.SQLite library from system.data.sqlite.org. Head over to their download section and download the libraries that best suit your need depending on the .NET Framework you want to target and the Windows bit version.

Extract the file and add System.Data.SQLite.dll as a reference in your project. Keep in mind that SQLite.Interop.dll also needs to be in your executables directory but doesn’t need to be added as a reference in your project. Moreover, if your application is targeting Any CPU it is likely that you will get an exception. So make sure to navigate to Project properties -> Build and set the Platform target to the bit version of the System.Data.SQLite.dll binary you have downloaded.

Visual Studio application build settings - Platform target

Finally, the snippet below should give you the general idea on the main functions you will need to learn first, mainly

  • Creating a file for your database
  • Creating a table in your database
  • Inserting information in the database
  • Retrieving information from the database

The output will be

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

9 Responses to How to create and connect to an SQLite database in C#

  1. abullais b says:

    Thanks a lot,
    Given code working.

  2. George says:

    Thanks for article. Worked great, only made use of https://www.nuget.org/packages/System.Data.SQLite.Core/

  3. reza says:

    hello
    when I try to use
    SQLiteConnection.CreateFile
    Vs say: Active The name ‘SQLiteConnection.CreateFile’ does not exist in the current context.

  4. Dennis says:

    Thank you.

    Used Package Manager in Visual Studio and entered PM>Install-Package System.Data.SQLite

    Used your code and it worked first try! Simple and clean.

    It saved the sqlite database in the bin/Debug folder.

  5. devone says:

    Unable to load DLL SQLite.Interop.DLL : The specified module could not be found

    • CooLMinE says:

      Make sure SQLite.Interop.DLL is located in the same folder as your application and also that they are both (your application and the .dll) using the same architecture (32bit < --> 32bit).

  6. Robert Hana says:

    After more than an hour of searching for a developer who knows what they’re talking about, here you are. Excellent post, kudos to you. For other people new to C# out there, if you are using Visual Studio Express 2013, you can install SQLite by right clicking on “References” in your project, then go to “Manage NuGet packages” type SQLite in the search, wait for it to come up, then click install. The entire code placed under a button press event (put a button on a form, and double click it, put the code between those brackets.) One last thing, you will need to add “using System.Data.SQLite” at the top of the code.

    • Jim Lammy says:

      Robert said it all except for one thing; Robert’s comment provided the key I needed to get this to work. So I guess there is hope for even us total noobs. Great post CooLMinE, thanks to you both.

    • jacob says:

      I tinkered with this for several hours using a different tutorial. I kept getting “SQLite is not in System.Data, are you missing an assembly reference?” I had a reference; I had using statement. I tried downloading and installing SQLite separately and through NuGet command line for my .NET version using both x86 and x64 and setting CPU target. Your steps worked. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *