LocalDB connection string to v14 SQL - sql-server

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"

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

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.

Cannot connect Docker MsSql from AspNetCore app

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;"
}
}

added migration , but database is not created

migration is added and the connection string is correct but the database is not created
what should i do
help me please
i closed the sql server and re-opened it but nothing is changed
my connection string :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;
Initial Catalog=Final_BD;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
http://i.stack.imgur.com/SR3Qf.png
when i try updata-database it shows me an error message :
" 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) "
http://i.stack.imgur.com/9oQOR.png
Try changing your connection string as:
"Data Source=.\SQLSERVER;
Initial Catalog=Final_BD;Integrated Security=True"
providerName="System.Data.SqlClient"
when i try updata-database it shows me an error message :
" 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) "
http://i.stack.imgur.com/9oQOR.png

Why is my connection to a local SQL Server db failing?

Based on what I learned here, I'm using this connection string:
using (var conn = new SqlConnection(#"AttachDBFilename=C:\HoldingTank\AdventureWorksLT2012_Database\AdventureWorksLT2012_Data.MDF;Integrated Security=True;User Instance=true"))
...to (attempt to) attach to a local SQL Server db, but I get this exception at runtime:
System.Data.SqlClient.SqlException was unhandled by user code
HResult=-2146232060
Message=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)
Source=.Net SqlClient Data Provider
Based on my legacy MS Access connection string, I had previously also had:
Provider=System.Data.SqlClient;
prior to the "AttachDBFilename=..." part of the connection string, but that caused an exception of its own...
You are missing a few values in the connection string. Here is one I used recently:
"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
I'm thinking the most likely culprit is the lack of the "Initial Catalog" value.
The connection string you need will depend on several factors, such as the edition of SQL Server (LocalDB/Express/Standard), whether or not it's a named instance, and the type of authentication (SQL vs Integrated) you have in place.
The answer from #zippit is a good example of a connection string for LocalDB using integrated security.
The same string to a Sql Express server would look like this:
"Data Source=serverNameOrIpAddress\sqlepxress;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
..and to a standard edition of Sql Server would look like this:
"Data Source=serverNameOrIpAddress\sqlepxress;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
..and to a named instance on a standard edition of Sql Server would look like this:
"Data Source=serverNameOrIpAddress\instanceName;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
All those assume integrated authentication. If you have sql authentication set up, you would substitute "Integrated Security=True" with "User Id=username; Password=pword;"
Also, if the sql server is on the same machine, you can use this for the Data Source parameter for Sql Express
.\sqlexpress
..and this for standard Sql Server
(local)
Here's a site I've found useful: Connection Strings

Resources