How to Add and Use a Database in a Simple C# Project

For many new C# developers, one of the biggest turning points is learning how to connect a program to a real database. Up to this point, most projects rely on in-memory collections — lists, dictionaries, or arrays — to store data. But once your data needs to persist beyond the life of the application, a database becomes essential.

In this article, we’ll walk through how to add and use a simple SQLite database in a C# project. SQLite is lightweight, serverless, and incredibly easy to set up — making it perfect for beginners who want to learn database concepts without the complexity of configuring SQL Server or MySQL.

By the end, you’ll understand how to create a database, define tables, insert and retrieve data, and integrate it all seamlessly into your C# project.

Why SQLite?

SQLite is one of the most popular embedded databases in the world. It’s used in mobile apps, browsers, and even operating systems. Unlike most databases, SQLite doesn’t require you to install a separate server or configure network access — it stores your data in a single file that lives alongside your project.

This makes it perfect for:

  • Small desktop or console applications

  • Learning how to work with databases in C#

  • Prototyping projects before scaling up to larger databases later

In short, SQLite is the simplest way to learn the core database workflow in C#: connect, query, store, retrieve.

Setting Up the Project

We’ll start from scratch with a new console application, but these steps also apply to WinForms, WPF, or even ASP.NET Core projects.

1. Create a new C# project

Open your terminal or Visual Studio and create a simple console app:

Creating an app from the command line

2. Add the SQLite NuGet package

To use SQLite, we’ll need the System.Data.SQLite package (or alternatively Microsoft.Data.Sqlite, which is newer and slightly simpler). For this example, we’ll use the Microsoft.Data.Sqlite package:

There are so many useful packages available to you

This installs the library that lets you talk to SQLite databases directly from C#. Please note that you can install Visual Studio to work with this new project in the integrated development environment (IDE). I won’t be covering the installation and setup process here, but I don’t think you will find it difficult. Respond in the comments of this blog post, email me, or message me on Skool if you need help.

Creating the Database

Unlike SQL Server, SQLite doesn’t need a running server. You can just point it to a file, and if that file doesn’t exist, SQLite will automatically create it for you.

Let’s start with some simple code that creates a new database file named app.db.

My app.db file was created in the bin/Debug/net6.0 folder. You may need to download the SQLite Viewer extension in Visual Studio to view that file.

When you run this program, SQLite creates a file named app.db in your project folder. Congratulations — you’ve already created a working database!

Creating a Table

Now that we have a database, let’s define a table. Think of a table like a spreadsheet: each row represents one record, and each column represents a piece of data about that record.

For this example, we’ll store a simple list of Users with three columns: Id, Name, and Email.

Creating our table for data

This command ensures that the table exists before you try to use it. If it already exists, SQLite simply skips this step — which makes your program safe to run multiple times.

Inserting Data

Now that the Users table exists, let’s add some data. Here’s how to insert a new record into the table:

Inserting a record into our new table

Why use parameters?

You might notice we used @Name and @Email instead of inserting values directly into the SQL string. This technique, called parameterization, protects your program from SQL injection — a common security issue where malicious input could alter your queries.

It’s a best practice to always use parameters when building SQL commands in C#.

Reading Data

Let’s verify that our data was inserted correctly. You can read data back from the database using a SELECT statement and a data reader.

Reading the newly inserted table data

This reads every row in the Users table and prints it to the console.

When you run the program now, you’ll see:

Written to the console

Updating Data

If you want to update an existing record, you can use an UPDATE statement. Let’s say Alice changed her email address:

Give Alice a new email

If you now run the select query again, you’ll see Alice’s updated email address.

Deleting Data

Finally, let’s look at how to remove data from the database.

Bye bye, Alice

Wrapping It All Together

Here’s a slightly more complete example that puts everything together in a single program, in the following 3 images:

For the Console.WriteLine… line, add the characters: “); to the end. The long line got cut off, I apologize. Two more closing curly braces should appear at the end too, to close the Main function and Program class.

This is the foundation for nearly every database-driven C# application you’ll ever write.

Expanding Beyond SQLite

Once you understand this pattern, you can swap out SQLite for any other database provider. For example:

  • SQL Server — perfect for enterprise and larger-scale apps.

  • PostgreSQL — great open-source option with advanced features.

  • MySQL — a popular choice for web apps and cross-platform projects.

Most of these databases have compatible C# packages with nearly identical code for connecting, inserting, updating, and selecting data — just with slightly different connection strings.

When to Use an ORM

You might have heard of Entity Framework Core (EF Core) — an ORM (Object-Relational Mapper) for .NET. It lets you work with databases using C# classes and LINQ instead of writing raw SQL.

For example, you could define a User class and then query the database with code like:

Nice concise syntax

That’s powerful and convenient — but it adds complexity, and you lose a bit of control over what SQL is being executed.

For beginners, I recommend starting with raw ADO.NET (like we did above) so you understand how the communication between C# and the database really works. Once you’re comfortable with that, tools like EF Core or Dapper can simplify your workflow tremendously.

Common Pitfalls

Let’s wrap up with a few common mistakes and how to avoid them:

  1. Forgetting to close connections:
    Always use using statements when working with database connections, commands, and readers. They ensure that resources are disposed of properly.

  2. Hardcoding paths:
    SQLite databases are files. Store them in a known directory or use a relative path (Data Source=app.db) for portability.

  3. Skipping parameterization:
    Never concatenate user input into SQL strings. Always use parameters to prevent SQL injection.

  4. Not handling exceptions:
    Wrap your database code in try-catch blocks to handle errors gracefully.

Conclusion

Adding a database to your C# project doesn’t have to be intimidating. With SQLite, you can create, query, and update a real database in just a few lines of code — no server setup, no configuration headaches.

This approach gives you hands-on experience with SQL and helps you understand how your programs persist and organize data. Once you’ve mastered the basics, you’ll be ready to graduate to larger databases and even ORMs like Entity Framework Core.

Start simple, experiment often, and build something that saves real data.

Because that’s when your C# projects start to feel like real software.

Previous
Previous

Move Over JavaScript — C# Can Build Full Websites Too

Next
Next

Understanding Regular Expressions in C#: A Practical Guide to Powerful Text Matching