Can't open connection to SQL server - c#

When I try to connect to the MS SQL server in the local domain with SQL with SQL server authentication using the following code fails:
SqlConnection sql = new SqlConnection(conString);
sql.Open();
sql.Close();
I can connect to my local SQLExpress test database just fine, using either windows or sql authentication. I tried using many different connections strings for the domain database, including using the connection string generated by (successfully) adding the server as a DataSource. What am I doing wrong?
EDIT: When I add the server in the server explorer of visual studio, I can connect successfully. I would assume that the connection string thereby generated is valid:
Data Source=mySubDomain.myDomain.local;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUser;Password=myPassword;
I checked and remote connections are allowed. I do not have permission to access the windows server which the sql server is running on, so I can't check any further settings.
EDIT 2: The following message comes with 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)
EDIT 3:
I tested my application on a different computer in the same domain, and it worked. Does someone have a clue what's going on or how I could find out?

The Group Policy or firewall (or both, not sure) of this company domain make my .exe file (and any other .exe) behave in the following way:
Launching it from C:\Program Files (or C:\Program Files (x86)) as any user works fine
Launching it from anywhere else as a normal user is not allowed and fails
Launching it from anywhere else as a local admin will execute the application, but connections are blocked

Related

Switching from SQLite to SQL Server?

I'm trying to simply switch from SQLite over to Microsoft SQL Server on a basic .NET 5.0 application for local testing purposes.
I'm on Windows 10 running a linux subshell on Visual Studio Code.
I've installed Microsoft SQL Server, and also the SQL Server Configuration Manager as well as SQL Server Management Studio.
I've created a database via SSMS named wmsDB.
And in my Visual Studio Code connection string I have the following:
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-P24QHEM\\SQLEXPRESS;Database=wmsDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Every time I run dotnet run and try to sign up via email and password I'm greeted with the below error:
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database 'wmsDB' on server 'DESKTOP-P24QHEM\SQLEXPRESS'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'miniwms.Data.ApplicationDbContext'.
Microsoft.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: 25 - Connection string is not valid)
I'm not sure what I'm doing wrong - I've tried a few variations of the server name that I was hoping would fix it but still I'm encountering this issue every time.
If you trying to connect from Linux, you'll have to use TCP/IP. By default SQLEXPRESS doesn't have it enabled. Use the SQL Server configuration manager to enable it and set the SQL instance to listen on a fixed port. Use 1433 as it's is the default if you don't have any other SQL Server instances installed.

web service - SQL connection string

I've built an .asmx web service which retrieves informations from a local sql server 2014 database.
Everything is working fine on localhost, but after publishing the web service to Azure i get the error:
An unhandled exception of type 'System.Web.Services.Protocols.SoapException'
occurred in System.Web.Services.dll
Additional information: Server was unable to process request. ---> 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)
SQL Server is configured to allow remote connections.
My connection string is like:
string con2 = #"Data Source=OfficePc\MSSQLSERVER2014;Initial Catalog=Database;Persist Security Info=True;User ID=Admin;Password=123456";
Is the error the result of something missing from the connection string, or am i missing some configurations changes?
As Paul mentioned in a comment under the question, your connection string is pointing to a local database resource (presumably on your dev machine). Even though you configured your local database server to support remote connections, the address OfficePc\MSSQLSERVER2014 isn't addressable, as that does not equate to a machine address (IP address).
Your app would need to connect to your database via an accessible IP address (which might require you to do some port-forwarding on your local network, or open ports on your firewall).
Alternatively, you can migrate your database to Azure (either with SQL Server in a VM or with the SQL Database service).
Keep in mind: If you are accessing a local (on-premises) database server from Azure, there will be latency added, as well as some outbound bandwidth costs.
It looks like SQL Server instance is not running or not accessible. Try connecting to the same database using SSMS and if you get the same error then the instance is not running.
Mostly the error occurs when the Database server was not found. Recheck if the server name (Data Source) is mentioned correctly. If you manually generated the connection string use .uld file to generate connection string.
To auto generate connection string using .udl file:
Create a sampe.txt file.
Rename it as sample.udl file.
Then double click on it, It will show you window entitled 'Data Link Properties'.
Configure the connection there.
Then Test the connection using test connection button.
Then open the file with notepad. It will show you the exact connection string.
For further reference check : MSDN

Cannot connect to any remote database using SqlConnection

I cannot connect to a particular SQL Server 2008 database server from C#.
I am able to connect using SSMS, and run queries using SQLCMD, but when I try to connect from C# using the SqlConnection it fails to open the connection with a 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)
Here is my code:
using (SqlConnection conn = new SqlConnection(#"Server=LDNPSM050000137\PLN000000014T;Initial Catalog=MiscData;Integrated Security=True;"))
{
//exception occurs on this line
conn.Open();
//use connection
conn.Close();
}
I get a similar response using ODBC:
string connectionString = #"Driver={SQL Server Native Client 10.0};Server=LDNPSM050000137\PLN000000014T;Database=MiscData;Trusted_Connection=yes;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection.Open();
}
This fails with an OdbcExcpetion:
ERROR [08001] [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
ERROR [HYT00] [Microsoft][SQL Server Native Client 10.0]Login timeout expired
ERROR [08001] [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
I have checked the server and instance name are correct, and the server is configured to allow remote connections because I can connect through SSMS. Does anybody have a suggestion of what the problem could be or how to resolve this?
Update:
In case this helps somebody diagnose the problem - This is specific to my machine/user account. My colleague can run the code fine from his machine. I am also able to connect to my local instance using a connectionstring with the appropriate changes to the Server and Initial Catalog.
The error occurs for all remote database servers.
I'm not sure if this is specific to my company's specific IT infrastructure - but the reason this was not working for me was because the project was saved in My Documents which stored on a network share. Apparently .NET will not let you connect to a remote database server when the executing code is located on a network share - it work fine when I copied the project to my local drive. I am using Windows 7 and Visual Studio 2012.
Network shares by default have partial trust, so things that work when a project is on your local drive, e.g. connecting to remote server, won't from network location.
You can give the network location full trust (https://msdn.microsoft.com/en-us/library/zdc263t0(VS.80).aspx) or move your project onto your local drive.
I think the problem is the Server value in your Connect string:
From MSDN
I think you need to do this:
Server=tcp:LDNPSM050000137\PLN000000014T
Server=np:LDNPSM050000137\PLN000000014T
Depending upon whether you want to use TCP or Named Pipes...

SQL Error : error: 40 - Could not open a connection to SQL Server

I Get this error message whenever I tried to up my aspx page.
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
whic is connected in this connection string
SqlConnection conn = new SqlConnection("Data Source=192.168.xxx.xxx;Initial Catalog=DBSample;User ID=dev;Password=pass;Integrated Security=SSPI;"))
The weird thing is that the server that I'm connecting has already hosting some aspx page. I don't knnow if there's missing in my connectiong string Thanks. and I know the server that I'm connecting to is already allowed remote connection since it's already hosted some aspx websites. :(
Thanks!
Do you need to add an instance name to your connection string? Do you have the SQLBrowser service running on the target machine, or do you have to specify a port for the instance?
You also get that very same error when the database doesn't exist at the location that you are trying to connect to. Have tried looking at the connection strings of the aspx pages that are successfully connecting?
edited: Specifying Integrated Security=SSPI means you will be using Windows authentication to login to the database. What user is your aspx page running as (check your app pool)? Does it have the rights to log in to the database? This could also explain why it works on one server but not another.
Are you trying to connect to a hosted SQL Server over TCP/IP?
The reason I ask is some firewalls block traffic over Port 1433.
If not then it is simply a case of validating the connection string details and ensuring the SQL Server Engine is actually running...
Have you EVER been able to connect to this database from the PC you are currently attempting this connection on?

Sql Server 2005 localhost System DSN not working

I've got an C# .Net 3.5 executable that runs on a local machine and makes database calls to a server running SQL Server 2005. I've added a System DSN using the Data Sources manager on the local machine called "localserver" (with the driver set to SQL Server). When I create the DSN, I can test the connection to the server successfully.
I've added localserver to my connection string in my executable, and I get an 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)"
The server I am connecting to does allow for remote connections. When I replace localserver with the server name, it connects fine, but this program will be at multiple locations with multiple server names, so I need to make the data source dynamic.
Any ideas as to why this is happening?
Thank you,
Aaron
EDIT:
I've also tried using "localserver,1433" as my data source, but 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: TCP Provider, error: 0 - No such host is known.)"
EDIT:
Thank you for your responses. To solve my problem, I made a small method to gather the servername using an odbc connection.
I can then send the servername to the SqlConnection. Thanks again for your input.
SqlClient (ie. SqlConnection) has absolutely nothing to do with ODBC. As such using an ODBC Data source Name in the SqlClient connection string will get you nowhere fast.
You can configure the server name in app.config and build the connection string using SqlConnectionStringBuilder. At deployment, you change the exe's or the user's .config file appropriately.
As Remus said, DSN has nothing to do with SqlConnection. Instead use this connection string:
http://www.connectionstrings.com/sql-server-2005#1
Also read this:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
An Excerpt from the above post:
To connect to a local computer,
specify "(local)" for the server. If a
server name is not specified, a
connection will be attempted to the
default instance on the local
computer.
I would repeat that SqlConnection has nothing to do with DSN,

Categories