© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
14 replies
Adam

Trouble Connecting to Database

I'm using Dapper and SqlClient but I keep getting this error:

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)

here is the code:
using System.Data;
using Dapper;
using Microsoft.Data.SqlClient;

namespace MacBlazor2
{
    public class DbHelper
    {
        private IDbConnection connection;

        public DbHelper() {
            connection = new SqlConnection("User ID=postgres;Password=password;Server=localhost:5432;Database=dvdrental;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;");
            Console.WriteLine(connection.Database);
            Console.WriteLine(connection.ConnectionString);
            foreach(Person p in connection.Query<Person>("SELECT actor_id, first_name, last_name FROM actor")) Console.WriteLine(p.first_name + " " + p.last_name);
        }

    }
}
using System.Data;
using Dapper;
using Microsoft.Data.SqlClient;

namespace MacBlazor2
{
    public class DbHelper
    {
        private IDbConnection connection;

        public DbHelper() {
            connection = new SqlConnection("User ID=postgres;Password=password;Server=localhost:5432;Database=dvdrental;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;");
            Console.WriteLine(connection.Database);
            Console.WriteLine(connection.ConnectionString);
            foreach(Person p in connection.Query<Person>("SELECT actor_id, first_name, last_name FROM actor")) Console.WriteLine(p.first_name + " " + p.last_name);
        }

    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Trouble connecting to MySQL database form a seperate computer
C#CC# / help
2y ago
Connecting to database via String in XML
C#CC# / help
2y ago
✅ Need help connecting to SQL Database on .NET
C#CC# / help
5mo ago
✅ Connecting to a database + Extracting Data from it.
C#CC# / help
2y ago