C# Console Database General

Creating SQLite Database and Table in C# Console

In this tutorial I will show  you step by step how to create a SQLite database and how to add a table in the database in C# Console Application.

Before  open and download SQLite DLL in your solution for connectting  to sqlite database  in C#, And you will learn step by step how to do this.

 

Step 1: Open Visual Studio and create a C# Console Application. File / New /  Project  -> C# / C# Console Application.

 

Step 2: Right click on the “Soliton Name” in the Soliton Explorer. Soliton explorer is on the right handside on the Visual Studio. If you can’t see Soliton explorer , click on the View Menu and you will see Solution Explorer in the View Menu.

After right clicking the Solution Name, click Manage Nuget Package… as in the following

 

Step 3 : Find the SQLite into Nuget packages and click install button.

 

Step 4: Create Sample class as following images.

 

Step 4: If database does not exist, CreateDatabaseAndTable method will automatically create the database and tables. If database exists, there are no affect any tables and the database.  (C# SQLite Create Table if not exists )

And don’t forget, creating SQLiteConnection, SQLCommand objects before the CreateDatabaseTable

 

Step 5: If you want to add some data and select them in the C# Console add  this methods in the Sample class.

 

Step 6: From the end of the Sample class will be as follows

 

Step 7: In main method you can add some data and query them

 

Output:

 

 

2 Comments

  • I do not understand Step 4.

    “Step 4: If database does not exist, CreateDatabaseAndTable method will automatically create the database and tables. If database exists, there are no affect any tables and the database. (C# SQLite Create Table if not exists )

    And don’t forget, creating SQLiteConnection, SQLCommand objects before the CreateDatabaseTable”

    Where do I add this?

    Seems like a big jump form Step 3.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.