SQLException cannot connect in ASP.NET MVC [duplicate] - c#

This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Why did a network-related or instance-specific error occur while establishing a connection to SQL Server?
(15 answers)
A network-related or instance-specific error occurred while establishing a connection to SQL Server [closed]
(1 answer)
update-database: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"
(5 answers)
Closed 5 years ago.
I have moved a project that used to work fine but now it cannot acces my database anymore. I use Entity Framework and have connected to the database in SQL Server 2012 successfully. Here is what my connection string looks like :
<connectionStrings>
<add name="PrincipalServerContext"
connectionString="Data Source=ServerName\SQLEXPRESS;Initial Catalog=PrincipalServerDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
When I launch the website, after authentication I retrieve data, but it throws the exception
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
I believe it might have to do with several things but am not sure, and don't know how to fix them :
User permissions in SQL Server
Connection strings could be wrong but I checked and re checked a hundred times
Could not make it work like this, and the exception is throw on Linq queries to access DB.
EDIT I did try to publish the web site to test, and have a second connection string :
<connectionStrings>
<add name="PrincipalServerContext_DatabasePublish"
connectionString="Data Source=ServerName\SQLEXPRESS;Initial Catalog=PrincipalServerDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Though I'm just trying to debug on the new computer I set up the project, if I remove this line, I have an exception. So maybe my program is searching for this DB and finds nothing?
System.InvalidOperationException cannot find PrincipalServerContext_DatabasePublish connection string
(I translate from french)

It likely isn't related to user permission. Rather, the new server you moved the project to cannot access the server SQL Server is located on.
If the new server has telnet, try this in Command Prompt on the Application Server:
telnet serverName 1433
it'll likely hang there trying to connect. Few things you could do:
Make sure there is no firewall blockage between the two servers if they are on different zones.
Check if SQL Server's Server has an inbound rule open for port 1433 (SQL Server Express doesn't open this port by default, but Enterprise and Standard do).
Check to see SQL Server is accepting TCP IP and Named Pipe connections through Sql Server Configuration Manager.

Related

A network-related or instance-specific error occurred while establishing a connection to SQL GRAPH API ADO

I am currently creating an application with the Graph API and with access to the AD in .NET ASP, everything works correctly in debug mode, but when trying to publish it in an IIS or in Azure as WebApp the error occurs after logging in with the account O365 and redirect to my homepage:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
That happens when trying to generate the cache:
private ApplicationDbContext db = new ApplicationDbContext();
//mas codigo aqui
.
.
.
Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);
this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits, "ADALCache"));
The db was generated in an automatic way, so I don't know how it works quite well:
<add name="DefaultConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20190809024809.mdf;User Instance=true;Initial Catalog=aspnet-WebApplication2-20190809024809;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Already try to add the instance MSSQLLocalDB including replace (localdb) with v11.0, the np pipe, change to the name of the server in an IIS and none of this works still marking the same error.
In the case of Azure I understand that there is no localdb so I don't know how it would have to be modified to make it work
I also tried to connect a server but the program itself creates a .mdf:
WebApplication2.Models.ApplicationDbContext
and
WebApplication2.Models.ApplicationDbContext_log

Problems with SQL Server connection in console app

The SQL server is located on a remote machine.
I can connect to it with MVC app.
I can connect to it with SSMS.
But I can not do it in console app.
Neither entity nor SqlConnection is working.
Connection string is the same as in console and mvc app (which works). What can it be? Any ideas?
Error I am getting
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: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server)'
What can it be?
<add name="DBContext" connectionString="data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient";
cn.Open();
Console.WriteLine(cn.State.ToString());
The problem is that console app uses 445 and 139 port. In terms of security. But why MVC app doesn't use it. And how I can I use it without opening this port. Even dividing db part in different library didn't work.

connecting database to server probably [duplicate]

This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Closed 3 years ago.
I tried making a site for a school project and when I go ahead and input values in the labels in the register page and send it starts loading for some time and then opens up the code in the server side:
SqlConnection conn = ConnectToDb(fileName);
in this line -----> conn.Open();
and this error msg appears:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)'
You should check your connection string and that you have enough permissions to connect to that database
This is likely because (1) you have the wrong connection string (Example: Wrong server name), or (2) you're loading the connection string from a missing/invalid configuration file. For example, if you're running in debug mode and you your application is trying to load *.debug.config file.
There are other reasons, like you are using NT Authentication and the identity your application is running under does not have SQL Server access.

Sitecore Helix custom database: A network-related or instance-specific error occurred while establishing a connection to SQL Server

In my Sitecore Helix environment, I am trying to setup a custom database or at least setup a custom table.
In the following route:
C:\inetpub\wwwroot\habitat.dev.local\App_Config I've added the following line to my ConnectionStrings.config:
<add name="RulesDBEntities" connectionString="metadata=res://*/Models.Database.RulesDBModel.csdl|res://*/Models.Database.RulesDBModel.ssdl|res://*/Models.Database.RulesDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\RulesDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
When I load in the page, the following error appears:
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: SQL
Network Interfaces, error: 50 - Local Database Runtime error occurred.
Cannot create an automatic instance. See the Windows Application event
log for error details.
This also happens if I am only calling the database.Connection.Open(); method. I've been trying to find solutions, but I can't manage to fix it. I want to push information into a Database or just a custom Table inside my Sitecore environment.

C# Update Connection String Visual Studio

I am having some connectivity issues with Visual Studio and SQL Server.
This is the connections string in my web.config file;
<connectionStrings>
<add name="WintipToysConnectionString" connectionString="Data
Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated
Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
According to my DB connection string found in Visual Studio, my connection string is;
Data Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated Security=True
Which seems to add up - but, I get this error when my program hits a certain line;
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: SQL Network Interfaces, error: 50
- Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
I am actually following the WingTipToys tutorial on the Microsoft website and I am struggling to get it to use the Database I attached to SSMS.
I can write queries inside VS which works fine. So I don't really understand why my program is having this error when running?
Thanks.
Admittedly haven't used SQLExpress in a looong time. It looks like the syntax you're using is for an "instance" - which is pointed out in the error message:
...Cannot create an automatic instance...
What you have does look like server\instance conn string syntax...
Connect to the "default" instance of SQL Express like so: Data Source=.\\SQLExpress
Refer to this:
SSE Connection string sample
Hth...

Categories