SqlDependency/Query notification - SQL Server reboot - c#

I have an application running on a server which has a SqlDependency / query notification - monitoring changes on a table on a different server.
It works fine until we reboot/restart SQL Server. When SQL Server is rebooted due to some maintenance and patches, the other application throws the following errors and stops. I can definitely say it stops because it does not monitor changes once the SQL Server is up and running.
I have to restart the application to resubscribe to the query notification. I am not throwing any exception inside the code that would stop the application. I am catching the exception and sending an email.
System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.) --->
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.
I am new to SqlDependency / query notifications so I am not sure whether this is an expected behaviour or something I am doing wrong. It's my understanding(from other posts (SqlDependency Reliablity?) that I don't have to restart the job to resubscribe.
Appreciate your time and answers

This is normal because when you restart the sql server, the service was stopped and any application that is connected to sql server will be thrown this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
To avoid this problem, you can use a different instance for every application. You can install new server instances with different names to avoid this issue. Which when you restart a single sql server instance, the other sql server instance will still remain online.
You can follow these steps to install or create a new sql server instance:
https://4sysops.com/archives/how-to-create-a-new-sql-server-2012-instance/

Related

Cannot Connect to SQL Server from Another Computer

I have been going at this for hours, but have had no success with the current posts out there.
Tried methods:
Enabling TCP
Allowing Remote Connections for the database
Adding new inbound and outbound rules
All these things still do not let me login from my other computer. When I connect on my main computer, everything is instant, but when I tried on my secondary one, it takes a long time before giving me 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) (Microsoft SQL Server, Error: 1326)
Is there anyway of fixing this? I don't think watching more Youtube tutorials will help.

Intermittant SQL Server connection Issues

I maintain a system of automated data processing machines which run processes written in C# that are controlled by an SQl Server 2014 Enterprise Edition Database. Several times a day all of the processes on one machine will start receiving the following error:
An error has occurred. Details of the exception: 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) Unable to connect to SQL Server database.
All processes on the machine will experienc the same error when attempting to contact the database. After 10 or 15 minutes the machine will be able to connect again. While this is happening other machines on the same network will be able to connect no problem. We have checked the database - SQL Server is running - it is not blocked it is accepting connection, the instance name is correct. This is an intermittant problem that does not affect all machines connecting to the database but when it affects one - all processes experience the error. I have searched on this message and all the answers I have found are for constant failures - not intermittant like I am experiencing.
UPDATE
Found this on another question - Am testing now looks good:
The problem was that SQL was configured to allow both TCP and Named Pipes. Sometimes it would try and connect with the latter which doesn't use the standard SQL port. The solution was to prefix the data source/server in the connection string with Data Source=tcp:xxx.xxx.xx.xxx to always ensure it connects via TCP
The problem was that SQL was configured to allow both TCP and Named Pipes. Sometimes it would try and connect with the latter which doesn't use the standard SQL port. The solution was to prefix the data source/server in the connection string with Data Source=tcp:xxx.xxx.xx.xxx to always ensure it connects via TCP

Occasional Exceptions: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"

Occasionally our web application throws:
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
Now I have Googled maybe all kinds of variations regarding this error but all seem to point to the fact that the destination SQL server is either down or doesn't accept remote connections or something wrong with the connection string.
But my scenario is different, I can definitely reach the SQL Server (separate machine, same LAN) and this application runs normally most of the time, but in certain instances it starts to generate these errors. Then, apparently, they stop on their own then randomly they start again. Other applications on the same server to the same SQL Server (though different databases) throw these errors as well during the same period.
I have left a continuous ping between the two servers and I can confirm that during this period both servers have IP connectivity between them.
What could be the cause of this problem? What can I check next?
Did you make sure that TCP/IP is enabled under Sql Server Configuration management->SQL Server network config->protocols ?

Connecting to Azure database when running web application locally

I have been developing a basic Azure based web application and have run into a problem. The application queries a database that is hosted on Azure and returns the values to the user. When I run the application locally on my private wifi network and when I deploy the application to Azure, it runs fine and the queries are returned correctly. However, when I run the application locally on my companys wifi, I get the following error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: 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: 0 - No connection could be made because the target machine actively refused it.)
Can anyone tell me if there is any additional code I need to add or what exception I need to ask to be put in the company's firewall in order to allow me to run and test the application locally?

Desktop application cannot connect to database but web application can

I have written a C# desktop (winforms) application which is essentially a clone of a web application I wrote. The web application is able to talk back and forth with the database just fine. However, when I install the desktop application, I get the following error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server
I am using the exact same connection string in the desktop application as I am using in the web app which looks like this:
"Data Source=tcp:s08.winhost.com;Initial Catalog=THEDATABASE;
User ID=USERNAME;Password=******;Integrated Security=False;"
I am unable to use SQL Server Configuration Manager as my databases are hosted on Winhost.com. However they have made it clear to me that the proper ports are open, named pipes are on and SQL Browser is configured correctly. If anyone has had a similar problem, please let me know what you did to resolve this issue. I have also included the entire error below.
Thanks
************** Exception Text **************
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: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) ---> System.ComponentModel.Win32Exception (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Remove TCP protocol name and port number and try with full hosting website address..something like this
Data Source=www.winhost.com;Initial Catalog=THEDATABASE;
User ID=USERNAME;Password=**;Integrated Security=False;
Eventually I found that the problem was a proxy server. The application is being used by corporations who have proxy servers setup and due to this port 1433 was blocked. I ended up resolving the issue by creating a webservice.

Categories

Resources