Can't Connect to Azure SQL Server from Visual Studio Enterprise 2015 - c#

Okay, I am at a complete loss. I have set up a SQL server & database through Azure and have been trying to connect in Visual Studio. The database is listed under Azure in the Server Explorer in Visual Studio, but, when I try to open it in SQL Server Explorer, I get the following error when I try to connect:
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.) (Microsoft SQL Server, Error: 10060)
I do not know what else to do. I have opened up port 1433 on my local firewall and allowed access from my computer's IP through the SQL Server's firewall. I temporarily opened it to all ip addresses, which did not work. The SQL Azure says the database is online. Is there an extra step or fix that I missed?

Connecting to your SQL Database would require you to allow it to be accessible from your IP first.
Please check this MS doc about how to do so.
By default Azure SQL DBs are exposed over the internet just with a simple username/password. Connecting from outside of Azure requires white-listing your IP.
The very simple way to do so is to connect using SQL Management Studio, which detects that and allows you to add you IP address automatically to the firewall rules. Have a look here to see how this works.
Update to include comments:
If none of this works, your problem probably has to do with from where you are trying to access it.
If you are behind a company firewall, there could be rules prohibiting connecting to certain port ranges. Try to connect from a wifi hotspot over your mobile phone is the quickest way to figure out.

Related

Cannot connect to SQL Server Express database from an UWP app

I have a UWP app that needs to connect to a SQL Server Express database. On my dev box, I don't have any issues.
On the client PC, I installed SQL Server Express, created the database, setup the users and logins.
When I run the app, and try to connect to the database, 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: TCP Provider, error: 40 - Could not open a connection to SQL Server)
The code I'm using to connect is:
...
using (SqlConnection connection = new SqlConnection(ConnectionStringTextBox.Text.Trim()))
{
try
{
connection.Open();
MessageBox.Show("Connected successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
...
Using this same code on a desktop app, on the client machine, the connection succeeds.
I have set up the UWP app to use version 1809 of Windows 10.
The client and my dev box are both version 2004.
Package.appxmanifest file has the following capabilities checked:
Enterprise Authentication
Internet (Client & Server)
Internet (Client)
Private Networks (Client & Server)
Windows Firewall has the correct ports open. In SSMS, I have enabled allowing remote connections, enabled TCP.
I don't know what I'm doing wrong or what is going wrong. This same UWP app works great on my dev box.
Any advice is appreciated.
If you're trying to connect to a SQL Server instance on the same machine it won't work because UWP applications are sandboxed and cannot connect to localhost over TCP/UDP - while connections to remote machines will succeed. I think this is stupid, but it's By Design.
As a workaround, you can configure SQL Server to use Shared Memory or Named Pipes and those approaches should work.
Update:
I forgot that there is now a Capability you can add to your AppX manifest to enable local-loopback connections (only for TCP, it doesn't work for UDP).
You can try this example on how to connect to sql server in uwp app, and try to do the same steps on your client pc, hope it works:
https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases#first-set-up-your-solution
I tried everything I could find online, change sql server firewall settings, sql server tcp/ip settings, change sql server browser firewall settings, none of them worked for me.
I did read someone saying about turn on loopback capabilities in UWP, but I didn't see that option for the targeted window version I had. In the end, I finally got it working by running the following command
checknetisolation loopbackexempt -a -n=YourAppPackageName

ASP.NET bug with SQL Server [duplicate]

This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Closed 6 years ago.
Got 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Trying to populate the screen with a list of Products.
The bug
It’s reading the connection string ok otherwise it would tell us. It’s configured to except remote connections.
This document claims to have an exhaustive log of troubleshooting steps. Every step we take below this link is related to it. We’re using SQL Server 2014. http://social.technet.microsoft.com/wiki/contents/articles/2102.how-to-troubleshoot-connecting-to-the-sql-server-database-engine.aspx
It didn’t help. I did everything.
Enabled remote connections.
I used ipconfig as admin in console to verify IP address and pinged
all of them and my computer with the ping command.
They all reported ok. Tested connecting to sql server from visual
studio using as example “tcp:ipv4/myInstance.” Which was read ok.
Reinstalled SQL Server 2014. Nothing.
I reinstalled the whole OS. Didn’t work.
If you require more explicit infomation please ask me, I'll give as much detail as I can.
If you are sure that the specified instance name is correct you can check the following in the Sql Server Configuration Manager:
Firstly, check under protocols in the nodes on the left:
And make sure that both TCP/IP and Named Pipes are enabled.
If this is disabled on any of these nodes connections to the SQL server will fail.
Also under SQL server Services, make sure the browser service is enabled.
Also, is the server and the pc you are connecting from the same pc?

SqlConnection stuck on Named Pipes

I'm seeing the following error when attempting to open a DB connection from within my C# application. I realize this error has probably shown up on 100's of questions before. However, in this scenario the error is only showing up on C# apps running on my specific desktop PC. I've scoured the internet via Google and within this website, but I can't find a solution.
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)"
WHY THIS SCENARIO IS UNIQUE:
I am able to connect to the SQL server from SQL Server Management Studio (SSMS). This application works on another computer and is able to establish a connection to the SQL server from there. So this error scenario is specific to C# applications running on my specific desktop PC. Other apps work (SSMS). Other PC's work.
When I "sniff the wire" using WireShark, the trace shows me that my app is trying to connect via NamedPipes (i.e. \Server\IPC$). I can't seem to force it to use TCP/IP.
THINGS I'VE TRIED:
Re-installed.NET Framework
Re-installed Visual Studio (C# - Express version 2010)
Created an alias within cliconfg.exe.
Is there something I missed?
Here are Connection Strings I've tried...
Data Source=<servername>;Initial Catalog=HIE;Integrated Security=true
Server=tcp:10.240.11.81;Integrated Security=SSPI; database=HIE
Data Source=10.240.11.81,1433;Network Library=DBMSSOCN;Initial Catalog=HIE;User ID=<SqlUserIdThatISetUpAsSysAdmin>;Password=<password>
Here's the code snippet:
_conn = new SqlConnection(); // _conn declared globally
_conn.ConnectionString = <the connection string above>;
_conn.Open();
The most likely scenario is that Windows Firewall is block the SQL Server communication. From MSDN (an article about named pipes, but relevant nonetheless):
Microsoft Windows XP Service Pack 2 enables Windows Firewall, which
closes port 445 by default. Because Microsoft SQL Server communicates
over port 445, you must reopen the port if SQL Server is configured to
listen for incoming client connections using named pipes. For
information on configuring a firewall, see "How to: Configure a
Firewall for SQL Server Access" in SQL Server Books Online or review
your firewall documentation.
Another scenario is that the client configuration on the local machine is not configured correctly. From the run prompt, you can execute cliconfg (the SQL Server Client Configuration Utility) to see the enabled protocols and the aliases that are used for the protocols.
Either redefine your Data Source as suggested by usr's comment, or configure your SQL Server to allow TCP connections (Why would you not allow TCP connections anyway?).
Here is the link to enable TCP on your SQL Server (The accepted answer is where you want to look at):
Enable remote connections for SQL Server Express 2012
Are you running your code off of local disk or a network drive? I encountered a very similar problem where my network drive was not trusted in .Net and that was why my connections failed.

Unable to connect to sql server 2008 with c#

I have installed SQL Server on one PC and I have created a c# program on another computer to connect to the server. However, when I run my program, I get an exception saying that login failed for user 'testing'.
This is the code used to connect:
SqlConnection con = new SqlConnection("Data Source=IP,PORT;Network Library=DBMSSOCN;Initial Catalog=Stockinfo;uid=testing;pwd=testing");
I have created a login in the server with userid and password as testing. As for the server authentication, it has been set to SQL Server and Windows authentication. I have also created an exception for firewall. What have I done wrong?
Thanks in advance for your help!
EDIT 1: I am able to access the server with SQL Server authentication using the same userid and password on the computer running the server.
Do you have Sql Server Management Studio. If yes, then you can open it and right click the instance in question, go to Properties -> Connections -> Check "Allow remote connections to this server".
You might also have to enable TCP/IP Protocol to be enabled thru Configuration Manager.
Does it return 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)
You can check this link: enable remote connections in SQL Server 2008
Another reason is to check another computer's firewall if port sqlbrowser (or port 1433 is open) was added on its firewall.
UPDATE 1
user testing simply doesn't exist or you have supplied incorrect password
Login failed for user 'testing'.
That usually means that the user doesn't have access to that database.

Network related error while deploying a window application & used database to another pc

My window application works fine on my computer but when I install it on another computer a network-related error occurred...
I'm using sql server 2005, I have store the backup of the database, when i run it, it flags an error.
I'm using visual studio 10, C#,sql sever 2005
Error is..
"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)""
When this happens it means that the program cannot connect to SQL Server with information you provided.
If for example you are trying to connect my-server\sqlexpress, usually you have one of this issues:
There is no computer named my-server in your network!
SQL Server is not installed on my-server.
Your client computer cannot reach my-server from the network. You can check this using ping command.
The installed SQL Server instance name on my-server is not sqlexpress (case-insensitive).
You have not enabled TCP/IP protocol for your installation instance of SQL Server in "Sql Server Configuration Manager".
You have not restarted SQL Server service after enabling TCP/IP.
Your SQL Server instance is not default instance, and SQL Server Browser service is not running on the server.
Server's firewall is preventing the client to access the server. Try turning off the firewall on the server (only to test this case).
Consider this a checklist, and start from the beginning. Check them one by one. And I hope that you find the problem.

Categories