Cannot connect to local SQL Server Express from my ASP.NET web app

168 Views Asked by At

I am developing an ASP.NET web app on my Windows 11 machine. I have set up SQL Server Express to accept tcp connection, tested it using dbeaver and able to connect successfully.

But when I run my app, it cannot access the SQL Server Express instance:

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMTN0TE9V55V", Request id "0HMTN0TE9V55V:00000013": An unhandled exception was thrown by the application.

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: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)

This is my connection string:

"LocalConnection": "Data Source=localhost;User ID=sa;Password=mypaswd;Initial Catalog=ABL;Encrypt=True; TrustServerCertificate=True; ",

and my dbeaver connection:

enter image description here

What could be wrong and how can I fix this?

3

There are 3 best solutions below

1
ANIL ÇIRAY BÜLBÜL On

Step 1: Go to Start->Run->Services.msc.

Once the Services are open, select SQL Server and start it, as per the given screenshot, given below:

enter image description here

Once you do it, SQL server will be up and running again.

Another method:

The Host may be incorrect. Here's what fixed the problem for me:

Step 1: Step 1: TCP/IP under SQLEXPRESS Protocols in SQL Server Network Configuration:

enter image description here

Step 2: Then, under IP Addresses, the IP4 or another IP field should be as shown in the picture below.

enter image description here

and

enter image description here

Step 3: SQL Server restarts.

0
Albert D. Kallal On

What does your connection string look like?

Note that for a WHOPPING 10 YEARS now, SQL Server does NOT by default use port 1433 anymore!!!

So, your connection string should look like this:

 Data Source=.\sqlexpress;Initial Catalog=Test4;Integrated Security=True

So, launching the SQL configuration tool, you MUST NOW have both SQL Server running, and ALSO MUST NOW have the SQL "Browser" service running. These two: enter image description here

I suggest you try using the connection builder/wizard in Visual Studio, and see what the resulting connection string looks like. So, say from project settings in Visual Studio:

enter image description here

Since this is local development, then you can use "." for the Server name, and as noted, in most cases, the default instance of SQL Server will be SQLEXPRESS.

Hence, building a connection will look like this:

enter image description here

1
Aryabhima A. Rahman On

I found the answer here https://stackoverflow.com/a/69011111/13064444 apparently, since I am using wsl2 to develop my app, i need to adjust the firewall setting as well.