Can't access database from local server, but can from remote server - asp.net

I've been trying to setup an SQL Server 2016 Express instance for my ASP.NET Core app. I've gotten it to a point where I can connect to the database with a dev version on my local development machine, but not with a published version on the server (that's also hosting the SQL Server Instance)
I'll call the local dev version the "dev server", and the remote published version the "real server". Both servers access the same SQL Server instance
On the dev server I connect without issues to the SQL server with the following connectionString in my appSettings.json
Data Source=10.50.70.50;Integrated Security=False;User ID=voetbal;Password=*******;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
When I try to connect from the real server I get the 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: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
On the remote machine I've tried the following connectionStrings in both appsettings.json and as an environment variable:
With IP:
Data Source=10.50.70.50;Integrated Security=False;User ID=voetbal;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
With localhost instead of IP:
Data Source=localhost;Integrated Security=False;User ID=voetbal;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
With Local Machine name:
Data Source=WIN-DMM50393I9A;Integrated Security=False;User ID=voetbal;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
With Local Machine name and SQL instance name:
Data Source=WIN-DMM50393I9A\MSSQLLOCALDB;Integrated Security=False;User ID=voetbal;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
As localdb:
Data Source=(localdb)\\MSSQLLOCALDB;Integrated Security=False;User ID=voetbal;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
Then I thought, maybe it should be "Server" instead of "Data Source", so I tried them all again with that substitution. Still the same error.
So something tells me the connection string is not the issue here. Whenever I search the internet for the error, I only find posts about people trying to connect to an actual LocalDB instance, and not an SQL Server Express Instance. And if my app was indeed trying to connect to a LocalDB instance, then why is my dev server working with the database?
I don't know what else I can do to troubleshoot this issue. Any ideas?

Are you sure you have the ConnectionString defined for both environments: DEV and Production, regarding appsettings.Development.json and appsettings.json?
Could you try to log the connection string value on the server:
string conectionString = Configuration.GetConnectionString("DefaultConnection");

Related

SSMS is OK but Local IIS Cannot Connect to SQL Server

I'm getting the dreaded "A network-related or instance-specific error" error but only when attempting to connect from my local IIS server. IIS and SQL are both installed on the same machine.
SQL Server (EXPRESS) is running and the SQL Server Browser is also running. Client Protocols (Shared Memory, TCP/IP, and Named Pipes) are all enabled. I can connect and run queries via SSMS both locally and remotely using both Named Pipes and TCP using either Windows Authentication (My Account) or a SQL Server login.
Here is the connection string I am using in my web.config
Server=(local)\SQLEXPRESS;Database=MyDbName;User ID=MyUsername;Password=MyPassword"
Server=(local)\SQLEXPRESS;Database=MyDbName;Integrated Security=True;
Any recommendations are appreciated:
Win Server: 2016
IIS: 10.0.14393
SQL Server Express: 14.0.1
Solved: This was due to an additional connection string only used for Code Migration updates.

SQL server error when browsing from IIS in Windows Server 2008 R2

Server Error in '/' Application.
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: Name Pipes Provider, error: 26 - Error Locating Server/Instance Specified)
I have created an asp.net application in IIS, in a windows server 2008 R2. When I try to browse the application from the IIS it gives me the above mentioned error. I deployed the app from Visual Studio using the FileSystem. The server and instance name are correct in the connectionString. The database exists in Microsoft SQL Server Management, I just want to connect and use it. Things I tried to fix this error:
Turning off the Windows Firewall and creating an Inbound Rule for the SQL TCP port 1433 and 90 (port 90 because I configured my app to use that port).
Enabling the TCP/IP protocol under SQL Server Network Configuration in (SQL Server Configuration Manager).
Starting the SQL Server and SQL Server Agent.
Here is my connection string:
<add name="sqlConnectionString" connectionString="Data Source=PRH-TEMP\SPARTANSOFT; Initial Catalog=Library; Integrated Security=True; User ID=sa; Password=******; Connect Timeout=15; Encrypt=False; TrustServerCertificate=False;" providerName="System.Data.SqlClient" />
I used Entity Framework Code First. My SQL Server allows remote connections. Server authentication: SQL Server and Windows Authentication mode.

Connecting SQL Server database to ASP.NET application release

I have an ASP.NET application using Entity Framework. I'm trying to release it on the hosting, but I get following errors:
[Win32Exception (0x80004005): The system cannot find the file specified]
[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: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]
I set the connection string as shown on the hosting website and created database. How should I configure this to get rid of this error?
Exception due to your web application is not able to connect SQLSERVER instance. Following are options to resolve the problem:
Check if your service is running up
Go to All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services
Check to make sure SQL Server service status is Running.
or
Open run command console(Windows + R) then type services.msc. You will all services in your system and check SqlServer (Sqlexpress) service is running mode.
Make sure your database engine is configured to accept remote connections
Start > All Programs > SQL Server 2005 > Configuration Tools > SQL Server Surface Area Configuration
Click on Surface Area Configuration for Services and Connections
Select the instance that is having a problem > Database Engine > Remote Connections
Enable local and remote connections
Restart instance using services.msc in run command window(discussed in #1)
Enable TCP/IP in SQL Server Configuration
Go to All Programs >> Microsoft SQL Server 2012 > Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP
Right Click on TCP/IP >> Click on Enable
Then restart the sqlserver service using services.msc in run command window(discussed in #1)
Lastly you need to check that it might be server firewall setting because sometimes firewall blocks incoming request from other servers. And also check you are providing correct connection string means username, password, server etc.
Thank You..
<connectionStrings>
<add name="conn2" connectionString="Data Source=192.?.?.?\your_db_instance;Initial Catalog=your_db_schema;Integrated Security=False;User ID=your_db_login;Password=??????" providerName="System.Data.SqlClient"/>
</connectionStrings>
As you are able to connect from SQL SMS, try to connect with the same remote parameters from Server Explorer window in Visual Studio as well. Click "add connection" on "Data Connections": Add Connection in Server Explorer. After the connection being created, get the connection string from the properties window by right clicking on the connection object. Use this connection string in the hosted web.config file.

Can't connect to SQL Server from asp.net application

Hi i want to publish my application in a IIS server.
First i create a database in my sql server and put all my needed data on it.
Then i update my web.config to use the new remote Database. Unfortunatly i get an error
ConnectionString:
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=IntranetDatabase;Data Source=SRVDEV2010\SRVDEV2010" />
Stacktrace :
[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: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]
I cheked if i can access database remotly. I create a udlfile to verify my connectionstring, Here is the result.
I don't know why the udl file can connect but not my application. Any help is appreciated
Under which user is the Application Pool configured to run? you're using Windows Authentication which means your IIS App pool should be configured to run under a user that has access to the DB.

Error connecting to SQL Server via alias from local IIS7

Just wondering if anyone has run into this before. Had a few devs look at this, and none of us can figure it out.
I have an ASP.NET Web Forms app connecting to a Sql Server db via an alias using EF4. The alias is set to point to my machine name (not "." or "local"). When I run it out of VS2012, it works fine. If I deploy it to my local instance of IIS7 and try to pull it up in a browser, I get the 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)
My connection string looks like this:
Data Source=aliasName;Initial Catalog=databaseName;Integrated Security=True;MultipleActiveResultSets=True
If I change the connection string so that the Data Source is my machine name instead of the alias, it works fine from IIS7. So, the problem appears to be something with the alias and only when running out of IIS7.
As a workaround, I am using a local build profile to deploy a different web.config that doesn't use the alias locally, but I would really like to figure out why it's not working.
Any advice would be appreciated.
Check this, specifically:
Pre-requisites for SQL Server Alias usage
You must be willing for your clients to connect to the instance via a network protocol. It is not possible to use shared memory connections that are available when the client and instance are on the same system. I use TCP/IP connections for my aliases.
On the system hosting the SQL Server instance you must have the SQL Server Browser service running (this is normally has Start Mode set to Automatic).
On a 64-bit system, if you have both 32-bit and 64-bit clients, you will need to create an alias for BOTH 32-bit and 64-bit clients.

Resources