Connect a .NET (C#) application to Neon
Set up a Neon project in seconds and connect from a .NET (C#) application
This guide describes how to create a Neon project and connect to it from a .NET (C#) application. We'll build a simple book library that demonstrates basic database operations using the Npgsql provider.
note
The same configuration steps can be used for any .NET application type, including ASP.NET Core Web API, MVC, Blazor, or Windows Forms applications.
To connect to Neon from a .NET application:
- Create a Neon Project
- Create a .NET project and add dependencies
- Store your Neon credentials
- Perform database operations
Create a Neon project
If you do not have one already, create a Neon project.
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify your project settings and click Create Project.
Create a .NET project and add dependencies
-
Create a new console application and change to the newly created directory:
IMPORTANT
Ensure you install package versions that match your .NET version. You can verify your .NET version at any time by running
dotnet --version
. -
Add the Npgsql NuGet package:
Store your Neon credentials
-
Create or update the
appsettings.json
file in the project directory with your Neon connection string: -
Add the configuration package to read the settings:
important
To ensure the security of your data, never commit your credentials to version control. Consider using user secrets or environment variables for development, and secure vault solutions for production.
Perform database operations
Step 1: Create table
The following code gets the connection string from appsettings.json
, establishes a connection to your Neon database, and creates a new table for storing books. We use the NpgsqlConnection
to open a connection and then execute a CREATE TABLE
statement using NpgsqlCommand's ExecuteNonQuery()
method. The table includes columns for the book's ID (automatically generated), title, author, and publication year.
Step 2: Add books
Next, we'll insert some books into our new table. We use an INSERT
statement with parameters to safely add books to the database. The ExecuteNonQuery()
method tells us how many books were added.
Step 3: List books
To retrieve our books, we'll use a SELECT
statement and read the results using a DataReader. The reader allows us to iterate through the results row by row, accessing each column value with the appropriate Get method based on its data type.
Step 4: Update books
To update books in our database, we use an UPDATE
statement with parameters to ensure the operation is performed safely. The ExecuteNonQuery()
method tells us how many books were updated.
Step 5: Remove books
To delete books from our database, we use a DELETE
statement with parameters to ensure the operation is performed safely. The ExecuteNonQuery()
method tells us how many books were deleted.
Best Practices
When working with Neon and .NET:
- Always use parameterized queries to prevent SQL injection
- Handle database exceptions appropriately
- Dispose of connections and commands properly using
using
statements - Keep your queries simple and focused
Source code
You can find the source code for the application described in this guide on GitHub.
Community Guides
Resources
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.