Cannot connect Docker MsSql from AspNetCore app - sql-server

I am trying to access Docker MsSql instance from my Aspnet Core app.
When I try from Management Studio, I can connect to the MsSql server. However, when I try to run the app, I get the following error message.
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)
Here is the connection string I am using from config.json
{
"ConnectionStrings": {
"MyConnectionString": "Data Source=127.0.0.1;Initial Catalog=Test;Integrated Security=False;Persist Security Info=True;User ID=SA;Password=##someStrongPass~~;"
}
}

I found the solution.
If anyone is running their AspNetCore app from the container and using a second container for the database instance. Make sure you use the host machine IP in the connection string and expose database port from the DB container. Unlike the containers on Linux, Docker containers on windows could not connect to the DB container's IP address directly. Hopefully, that can be addressed in the future releases.
So in this case, the connection string should be
{
"ConnectionStrings": {
"MyConnectionString": "Data Source=YOURMACHINEsIP;Initial Catalog=Test;Integrated Security=False;Persist Security Info=True;User ID=SA;Password=SomeStrongPass;"
}
}

You can also use the container name instead of the 127.0.0.1,port or localhost,port
{
"ConnectionStrings": {
"MyConnectionString": "Data Source=SQL_CONTAINER_NAME;Initial Catalog=Test;Integrated Security=False;Persist Security Info=True;User ID=SA;Password=SomeStrongPass;"
}
}

Related

EF Can't Find Database on Production

Info
Project is ASP.NET Core 5.0, ORM is EF, DB is SQL Server
Problem
On Development, our database is accessible but on production its not!
Connection String
Is set in appsettings.json and its:
"MyConnectionString": "Data Source=MyDomain.com;Initial Catalog=MyDB;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD"
When we run the project in Visual Studio with this connection string, everything is fine, but it fails when it's published to the host.
The error is:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Any suggestions?
Thanks in advance.
Certainly not in the production environment.
Asha, did you enter the string connection in the appsetting file in the Development section? Or in the production section

asp.net Core 3.1.5 app cannot access database on server from web deploy publish

I have a Blazor Web Assembly app which I have publishing to a server running Windows Server 2019 with the database on MSSQL Server 2019 Express Edition.
The web app is using AspNetCore 3.1.5 with EntityFrameworkCore 3.1.5. Locally the app runs fine through self-hosting and IIS Express through Visual Studio 2019.
I can also run migrations to the servers database and seed using the following connection string through the PCM using update-database;
"Data Source=IPADDRESS;Initial Catalog=DATABASENAME;Integrated
Security=False;Persist Security Info=False;User
ID=USER;Password=PASSWORD;MultipleActiveResultSets=true;"
however after I publish the app to the server, when I attempt to log into the app i receive the following error;
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
the only difference to the connection string on publish is that the IP become local host in my appsettings.release.json;
"Data Source=localhost;Initial Catalog=DATABASE;Integrated Security=False;Persist Security Info=False;User ID=USER;Password=PASSWORD;MultipleActiveResultSets=true;"
For completeness I have also attempted to add in the port number to the connection string;
"Data Source=localhost,1433;Initial Catalog=DATABASE;Integrated Security=False;Persist Security Info=False;User ID=USER;Password=PASSWORD;MultipleActiveResultSets=true;"
Finally I can confirm I can login with the same credentials using MSSQL Management Studio with localhost with RDP on the server.
I also attached the about info from management studio for information;
EDIT Updated Information
Following on from comments below here is a screenshot of the SQL Configuration Manager;
Can anyone direct me as to why its not connecting?

IIS can't connect to SQL Server, but an external PC can

My VB.Net code connects to SQL Server. On my PC, it succeeds. On IIS running on the same machine as SQL Server, it fails. Any ideas?
I've tested:
I've tried IP address and server name. Same result.
I've tried Integrated Security and I've tried username/password. Same result.
On the server machine, Excel and SSMS can connect to SQL server using the same server name, username and password.
The fact that my PC can connect excludes many common problems including: ip address, server name, port, firewall, turn on sql server, connection string, credentials, permissions, sql roles.
Server (IIS and SQL) info: VB.Net, connection string providerName="System.Data.SqlClient", Windows Server 2008 SP2.
My PC info: Run website in Visual Studio 2019. Connects to same DB using same connection string (not local db and not as a different user).
Code (fails on second line):
oConn = ConnectionService.DatabaseName.Connect
oConn.Open()
Connection string: connectionString="Data Source=IP Address\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=False;Persist Security Info=False;User ID=Username;Password=Password;" providerName="System.Data.SqlClient"
Error message: System.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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): The network path was not found
Oops. Right after posting this, I found out Web.Config pointed to a different connection string. That should explain and solve everything. I had checked Web.Config, but it turns out that that wasn't the one this server was using.

LocalDB connection string to v14 SQL

I created a DB like this:
C:\Users\user>sqllocaldb create demo
LocalDB instance "demo" created with version 14.0.1000.169.
C:\Users\user>sqllocaldb info demo
Name: demo
Version: 14.0.1000.169
Shared name:
Owner: PC\user
Auto-create: No
State: Running
Last start time: 13/03/2020 11:11:43
Instance pipe name:
C:\Users\user>
I Can connect to it with SQL Server 2017 Management studio using
(LocalDB)\demo
Windows Authentication
But I'm failing to create a connection string that can be used locally from within EF Core.
Some of my failed attempts:
"Data Source=(LocalDB)/demo;Initial Catalog=Catalog1;Integrated Security=True;"
"Data Source=(LocalDB)\demo;Initial Catalog=Catalog1;Integrated Security=True;"
"Data Source=.;Initial Catalog=(LocalDB)/demo;Integrated Security=True;"
"Data Source=.;Initial Catalog=(LocalDB)\demo;Integrated Security=True;"
"Data Source=.;Initial Catalog=demo;Integrated Security=True;"
"Data Source=.;Initial Catalog=demo;Integrated Security=True;"
This is always the error I get:
System.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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
System.ComponentModel.Win32Exception (53): The network path was not found
The instance is started, and SSMS can connect to it seamlessly, I think the only problem is that I don't know how to write the connection string properly
Attempt 2
Connections strings
"Server=(localdb)\\mssqllocaldb;Database=demo;Trusted_Connection=True;"
Tryied to add a user and use its credentials
CREATE LOGIN DEMO WITH PASSWORD = 'DEMO';
create user demoUser for login DEMO
And then tried to authenticate
"Server=(localdb)\\mssqllocaldb;Database=demo;Trusted_Connection=True;User Id=demoUser;Password=DEMO"

Local IIS Web server cannot connect to SQL Server

When I use the local IIS Web server, my ASP.NET MVC application is unable to connect to SQL Server. The database is on another server. However, if I use the Visual Studio Development Server, it works just fine.
Anyone know what settings I'm missing? Should something change in my Web.config?
Yes, TCP/IP is enabled on SQL Server.
Here is the error I get -
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:
Named Pipes Provider, error: 40 - Could not open a connection to SQL
Server)
Here is my connection string -
connectionString="
SERVER=servername;
Initial Catalog=dbname;
User ID=userid;
Password=passwd"
providerName="System.Data.SqlClient"
I think you need to use IP address to connect remote database something like SERVER=59.230.212.12 in connection string. Try to connect database in management studio from where your IIS is installed . then use same setting in connection string .

Resources