I have a C# application that needs to connect to a SQL Server DB hosted by Amazon's RDS. Fairly often it throws 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)
When it does this, it usually works on the next try. So I would like to add a programmatic "retry" to the queries in my app. I think that this (https://stackoverflow.com/a/4822976/1011724) looks like a good pattern to follow for this. However, that answer only retries for the following errors:
private enum RetryableSqlErrors
{
Timeout = -2,
NoLock = 1204,
Deadlock = 1205,
WordbreakerTimeout = 30053,
}
So is there a way to simply add this error? If so, what is the error code? Looking here (https://msdn.microsoft.com/en-us/library/cc645611.aspx), there is no error 40 and it also is not the exact wording of error -1.
Is my error a SqlException? If so what number? If not, how can I adapted the code from the answer linked above to account for this type of exception?
Related
So something very strange happened with this old C# application I started to work on. I copied it locally and I've been studying the codebase without making any changes. In fact, it ran locally every day for the past week. Again, no code changes or changing in the configuration, etc. Today, I wasn't able to run it anymore. I even cloned a fresh copy - no luck. Now it's giving me this error and I don't know why. The database is remote but I can see that it's working when RDPing. What could cause this?
public static SqlConnection OpenSqlConnection(dbConnections connection, string applicationName = null)
{
var conn = CreateSqlConnection(connection, applicationName);
conn.Open();
return conn;
}
In this line, I get the following error:
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)'
This Definitely looks like a network issue rather than anything to do with the code.
Check if Your SQL Server is accessible over the network
I have a EF Core 2 Dbcontext injected in the usual way into all Controllers of a Web API:
services.AddDbContext<KpiContext>(options =>
options.UseSqlServer(config.ConnectionStrings.KpiDB,
providerOptions => providerOptions.EnableRetryOnFailure()));
This is injected into controllers in the standard way as, say, kpidb
and then it's passed to a static method which does this:
somestaticmethod(KpiContext kpidb) {
// create something
kpidb.Add(something);
kpidb.SaveChanges();
}
This SaveChanges() intermittently fails with:
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: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified)
I can't see a pattern to this but it happens quite often, perhaps around 10% of the time. I've tried:
AddDbContextPool instead of AddDbContext
Using Transient scope for the context
simultaneous ping shows no network interruptions at time of failure
Using port for SQL instance to prevent enumeration makes no difference
Simple connect and SELECT every 2 seconds script from the source to target never fails
Nothing seems to make a difference. I know that DBContext isn't thread safe but there are no async EF calls to fall foul of.
Any ideas appreciated.
I am trying to create a new SQL DB in Visual Studio 2010, and Upon entering a Server Name and a DB name then pressing Ok, I get this 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)
Any ideas?
Just go through this article.I think you can solve the problem.
http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/
Also ensure you have specified servername and database name correctly in connection string.just take connection string from property window of added database.
Try this article, it goes through pretty much all the steps you will need to troubleshoot your connectivity problems:
http://social.technet.microsoft.com/wiki/contents/articles/2102.how-to-troubleshoot-connecting-to-the-sql-server-database-engine.aspx
I took an old software made by someone else, which need to connect to a database with the .mdf extension.
But when I run the software, I have an error 26. I have obviously checked Google and it seems the the main problem is the firewall. But if I disable it the problem doesn't dissapear.
Here is the code for connect the database :
DataClasses1DataContext db = new DataClasses1DataContext(Environment.CurrentDirectory + "\\bddgestionvin.mdf");
I've noticed, when I try to connect the database with the datacontext class it fail too with the same error 26. (see attached screenshot below)
The string connection used by this class is :
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bddgestionvin.mdf;Integrated Security=True;Connect `Timeout=30;User Instance=True`
I know I'm a little vague but I never did that kind of stuff (connect to a .mdf database) And the code is not mine, feel free the ask further explanation / code. Any help is welcome !
P.S The error message is
SqlException was unhandled 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)
(Sorry for the french software)
Hi I don't know you may see these steps. Make sure that these steps should pass.
The reason that we get this error message is the client stack could
not receive SSRP response UDP packet from SQL Browser. It's easy to
isolate the issue. Here are the steps:
1) Make sure your server name is correct, e.g., no typo on the name.
2) Make sure your instance name is correct and there is actually such
an instance on your target machine. [Update: Some application converts
\ to . If you are not sure about your application, please try both
Server\Instance and Server\Instance in your connection string]
3) Make sure the server machine is reachable, e.g, DNS can be resolve
correctly, you are able to ping the server (not always true).
4) Make sure SQL Browser service is running on the server.
5) If firewall is enabled on the server, you need to put
sqlbrowser.exe and/or UDP port 1434 into exception.
You can read more from here
You can also try PortQry and validate your firewall does not block SQL Browser UDP packet
A network-related or instance-specific error occurred while establishing a connection to SQL Server. Failed to establish a connection with SQL Server.
The server was not found or was not accessible.
May be server is stopped, you don't have permission to access the server, or the instance name is not correct .
Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
Please goto SSMS and try connecting the server. Verify the SQL Server is running and your account is configured to access the service.
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Thanks
I'm getting this error since I've moved my mvc3/entity framework site onto the live server from localhost. Normally when I would get this error, I'd check the database name, password and server are all correct in the connection string. I have checked this - they all seem fine.
I have aspnet Membership provider on the site within the database, and it allows me to login, verifies me, then tries to redirect me to another page, and then that's where the error happens - i.e., as soon as I connect to the database outwith the membership provider.
The database is on the same server as the site, and when I connect from localhost to the remote server, it works perfectly.
Here is the full error message:
Exception 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Here is the connection string:
<add name="ApplicationServices" connectionString="Data Source=192.168.1.43;Initial Catalog=BlueLadder;User Id=BlueLadderAdmin;Password=Auth1991;timeout=30" />
Anyone anything I could try?
Ok, despite my connection string in the web config, it was actually being ignored and the application was still trying to connect to a local version. Apparently you need to pass the connection string through the dbContext constructor, like so.
public Context()
: base("ConnectionString")
{
}
Question was answered here
Thanks for your help anyway.
Try removing the Data Source and Initial Catalog, and replace them with something like this:
Server=.\SQLEXPRESS;Database=BlueLadder;
I don't know if you're using SQLEXPRESS, so just modify accordingly if you're not.
Check if your server is accepting remote connections and has TCP/IP provider enabled. You can configure this using "Sql Server Configuration Manager" on your server
You could try specifying the named instance of your server. "Sql Server Configuration Manager" can tell you what instances are installed on your server.
Server = 192.168.1.43/MSSQLSERVER or
Server = 192.168.1.43/SQLEXPRESS