skip to Main Content

I have an ASP.NET Core 8 MVC project that I’ve containerized. Initially, I pulled a SQL Server image from Docker Hub and then modified the default connection as follows:

"ConnectionStrings": {
    "DefaultConnection": "Server=mysql-container,3306;Database=ProductsDB;User Id=root;Password=my-secret-pw;"
},

However, upon attempting to access the project through localhost on my web browser, I get the following error:

SqlException: 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

I’m seeking guidance on resolving this issue.

I have containerized the project and ensured that both containers are running on the same network in Docker.

2

Answers


  1. SqlException: 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

    The error you have encountered has many common reasons. For instance, it might be because of denial of connecton request. Database Server might not allow remote connections. You’ll need to configure it to accept connections from your application’s machine.

    Another important issue could be, firewall configuration.
    Check if the firewall on the SQL Server machine is blocking the connection on port 1433 (default SQL Server port). You might need to create an exception for your application or open port 1433.

    Apart from that, server name or instance name specified in your connection string might be incorrectly written or if there’s any typos. Ensure it’s spelled correctly and refers to the existing SQL Server. For your scenario, I have seen, your connection string seems to be for MySQL instead of SQL Server. Thus, if that’s the case, please update it to reflect SQL Server settings properly.

    Altough, above reason could cuase your remote connection failure. However, its hard to offer you any exact solution or and main reason what causing the issue.

    Instead, you could investigate steps by steps considering above aspect. In addition, if you have application log or SQL profiler you could check the details there as well.

    Note: I would highly recommend you to check this official document for more specific guidelines.

    Login or Signup to reply.
  2. SqlException: 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

    The One of main reasons for this issue is SQL Server Service is not running.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search