0

I'm attempting to connect to a local SQL Server Express instance I have on my dev box.

I'm using the following code:

var cs = new System.Data.SqlClient.SqlConnectionStringBuilder();
cs.InitialCatalog = "dev_db";
cs.DataSource = @"(local)\SQLEXPRESS";
cs.IntegratedSecurity = false;
cs.ConnectTimeout = 5;
cs.Encrypt = false;
cs.UserID = "user";
cs.Password = "password";

var xpto = new System.Data.SqlClient.SqlConnection(cs.ConnectionString);
xpto.Open();

This works fine in a console or Windows app, but fails miserably when trying in a Web API project running in a Docker Desktop (configured as Linux).

The exact same code.

This is the exception thrown:

System.Data.SqlClient.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: 25 - Connection string is not valid)

Has anyone encountered this problem and know how to solve it?

To preemptively answer questions:

  1. I also followed this answer to configure SQL Server Express.

  2. After reading this answer, I tried using host.docker.internal and using the IP address of my dev box. Both tries yielded the same results.

// Attempt with host.docker.internal
cs.DataSource = @"host.docker.internal\SQLEXPRESS"; 
// Same error

// Attempt with dev box IP
cs.DataSource = @"192.168.15.3\SQLEXPRESS";
// Same error.
  1. The instance of SQL Server Express is on the dev box, not in the Docker.

  2. SQL Server Express is configured to allow remote connections:

SQLExpress configuration

Edited to add:

This is the Docker file:

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["TestProject.csproj", "."]
RUN dotnet restore "./TestProject.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "TestProject.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestProject.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestProject.dll"]

Edited to add:

Connection with a PostgreSQL database was successful. Only SQL Server Express is giving me a headache.

11
  • Try disabling Windows Firewall (just as a test). Commented Oct 13, 2023 at 13:53
  • 1
    Is it SQLEXPRESS running on your docker container or it is on your local machine? Commented Oct 13, 2023 at 13:58
  • Can you show us your docker file? What ports do you have open on your docker container? Commented Oct 13, 2023 at 13:59
  • Since docker is a separate networking layer, you may need to explicitly open the SQL Server port in the Windows firewall. Commented Oct 13, 2023 at 14:18
  • Did you forward the ports into the SQL Server container using -p realport:containerport? Commented Oct 13, 2023 at 14:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.