Connect to SQL Local DB with C#/Dotnet

Like most tutorial-y posts I write, I couldn’t find a tutorial that was as simple as this turns out to be. Maybe it’s because I search on bing half the time…

Localdb is a component of Microsoft SQL Server Express, which is Microsoft’s free/development/small environment focused edition of SQL Server. To connect and use localdb in C# .NET we need to follow a few steps.

Set up the local db

In simple steps:

  • Download and run the installer for SQL Server Express from SQL Server Downloads | Microsoft
  • Make sure you select localdb in the installation feature selection page.
  • Once the installation is finished, open up a command prompt and do:
    sqllocaldb.exe info
  • This will list the local databases. By default you should have one called “MSSQLLocalDB”
  • If you want more information on a local database, just do sqllocaldb.exe info MyDatabaseName

  • If you don’t have a database or you want to make a new one, you can do
    sqllocaldb.exe create MyDatabaseName
  • By default, localdb stops the database after periods of inactivity. Do sqllocaldb.exe start MyDatabaseName

Connect from C# .NET

This was done in .NET 7, C# 11, and I started with a console app template but any should work.

Again, simple steps:

Make an App.Config File

Make an app.config XML file inside your project folder. This will be used to store your connection string.
App config created inside of project folder (Visual studio solution explorer)

Make your connection string

Connection strings for local databases follow this pattern: Data Source=(localdb)\MyDatabaseName;Integrated Security=true where MyDatabaseName is the name of your database that you can get by doing sqllocaldb info at the command line.

For example, mine is:

Data Source=(localdb)\Flashcardapp;Integrated Security=true

To connect to a local database called Flashcardapp.

Put your connection string and app.config together.

Your app.config file should end up looking like this:

XML
<?xml version="1.0"?>
<configuration>
	<connectionStrings>
		<add name="ConnectionStringName"
            connectionString="Data Source=(localdb)\MyDatabaseName;Integrated Security=true"
            providerName="System.Data.SqlClient"/>
	</connectionStrings>
</configuration>

Where MyDatabaseName is again the name of your database, and ConnectionStringName is the name you want to give your connection string, to reference it with.

Do the C# part now!

You’ll need to get the System.Data.SQLClient package and the System.Configuration.ConfigurationManager package from Nuget.

To get your connection string into C# you can do:

C#
using System.Configuration;

string myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

That will get the connection string from the app.config file and assign it’s value to a string – myConnectionString.

To actually connect to the database, you can add the following:

C#
using System.Configuration;

string myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
 using (SqlConnection connection = new SqlConnection(connectionString)) 
        {
            connection.Open();
            // You now have an open connection to the SQL database!!!!
        }

Of course, you probably want to actually do things with this connection. Here’s an example of a delete operation:

C#
using System.Configuration;

string myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
 using (SqlConnection connection = new SqlConnection(connectionString)) 
        {
            connection.Open();
            // You now have an open connection to the SQL database!!!!
            string query = "DELETE FROM table w;";
            SqlCommand command = new(query, connection);
            command.Parameters.AddWithValue("@name", name);
            command.ExecuteNonQuery();
        }

In

Leave a Reply

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