Can't access Azure SQL database with Visual Studio but can with SSMS - visual-studio

I'm trying to connect to an SQL database hosted on Azure from a function app I'm building in Visual Studio but I'm getting the following 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 - An attempt was made to access a socket in a way
forbidden by its access permissions.)
I can connect from the same PC using SSMS without a problem. I've tried connecting to the database using data connections in server explorer in VS but I'm getting the same error. If I connect to Azure with my account in server explorer it shows me a selection of services including SQL databases but there are none listed underneath although my account does have access to the database.
Are there any hidden gotchas I'm missing or settings within VS? I've also tried adding my client IP in the firewall settings but that didn't make a difference either.

After doing some more testing it turned out to be the client firewall that was blocking VS and VS Code from connecting to Azure SQL. It seems there was already an exception for SSMS.

The error message states "Verify that the instance name is correct"
It could be that the SSMS is automatically creating a fully qualified name once you say that it is an azure database.
If you use the short name from Visual Studio, then you cannot connect to the SQL server.
For example "myserver.database.windows.net" versus "myserver".

Related

SQL Server 2017 Connections working in ASP.NET C# Web projects, but not in Windows projects, in Visual Studio 2019

Using the basic SqlConnection code below, I have discovered that, at my workstation, I am unable to connect to our Sql Server 2017 deployment via ASP.NET C# code in any given type of Windows-based project, such as Winforms (.NET Framework or Core) or Console apps. However, this code will run without incident in any Web-based project, such as Web Forms or MVC, either using .NET Framework or Core.
protected void testConnection()
{
string con = "Server=MySqlServer;database=MyDatabase;Integrated Security=true";
using (SqlConnection cnn = new SqlConnection(con))
{
cnn.Open();
cnn.Close();
}
}
In a web project, this code runs. In a Windows project, I get this 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'
In investigating this problem, I have tried the following:
Explicitly declaring the domain on which my SQL server is found (eg. MySqlServer.domain.com). No good.
Explicitly declaring the SQL Server port number, according to the example shown on connectionstrings.com (eg. MySqlServer, 1433). We do use the standard port number, for the record. No good.
Changing the protection level of the test method (protected/private/public). No good.
Connecting to the SQL Server in PowerShell. This connection worked.
Pinging in the SQL Server in a command prompt. The server responded in 1ms consistently.
I have asked other users on this network to test this code in a Winforms project on their workstations. No one is able to reproduce my issue.
Consulted my sysadmin. He is so far not able to find any reason why I should be having this issue.
The only difference I have noticed in all of this is that, when I declare the port number, the Inner Exception returned with the error reads "Win32Exception: A non-recoverable error occurred during a database lookup". Otherwise, it simply reads "Access is Denied."
All of this tells me that the issue is obviously something peculiar to my workstation, but I have no idea what, apart from some firewall or other local security setting that the sysadmin neglected to check. Has anyone encountered this problem?
My VS 2019 installation is only one revision out of date as of this writing, 16.8.3 as opposed to 16.8.4. I am able to connect to and run TSQL code on any database I care to via SSMS. We connect using Windows Authentication. According to the results of "select ##version", our version of SQL Server is 2017, 14.0.3356.20 (x64).
Any insight is appreciated.
We fixed it by forcing Named Pipes to be enabled in SQL Server, as opposed to using the default setting.

Can't connect to TFS 2010 Analysis Services

I'm trying to setup reporting in TFS 2010.
I can't connect to the Analysis Services database and I also can't rebuild the Analysis services database.
I've tried to connect via TFS Admin console and SSMS and I'm getting this error in SSMS
Connect to Server
Cannot connect to xxx.com,1031.
ADDITIONAL INFORMATION:
A connection cannot be made. Ensure that the server is running. (Microsoft.AnalysisServices.AdomdClient)
The requested name is valid, but no data of the requested type was found (System)
and this error in TFS Admin
The SQL Server instance that you specified was not found or cannot be reached. Specify another server or instance.
The sql server analysis services service is running.
I used the following article to identify the correct port for SSAS service and I still cannot connect.
http://support.microsoft.com/kb/2466860
Any ideas would be appreciated.
It was the port number.
I fixed it by using the above link to identify the port number then connecting using TFS Admin tool.

Unable to start SQL server Agents

been trying to configure an ADO.NET connection for my Visual Studio application but I am running into issues and having no luck at all troubleshooting them. The major error that I run into is:
A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or not accessible. Verify the instance name is correct and that SQL server is configured to allow remote connections (Error 40: Could not open connection to the SQL server)
The steps I undertook to troubleshoot this are:
1. Open up Sql server configuration manager and under the SQL Server Network Configuration, Protocols for MSSQLSERVER,SQLExpress, SQL server 2008 connection string data source I enable each of the protocol names (Shared memory, Named pipes,TCP/IP,VIA)
As instructed I stop the SQL server services first under the SQL server services nodes.
Now I click start, MSSQL server service starts but both SQL Server 2008 and SQLEXPRESS agents do not. Stating the following:
The request failed or the service did not respond in a timely fashion. Consult the event log or other applicable error logs for details
This might just be the problem as when I attempt to create the connection string , the server name specified is MYWORLD/SQL SERVER 2008. Since the SQL server 2008 agent service refuses to start, the connection fails, leading to the first error message.
Does anybody have any leads on this and can let me know the necessary steps to mitigate this.
The SQL Browser service must be running to connect to a named instance.
Use SQL Servr Configuration Manager under Configuration Tools under Microsot SQL Server 2008 to assign the logon accounts for each service you want to run. Do NOT use any other method to assign user accounts because the correct rights will not be enabled.

Connecting to database on web host in Visual Studio or Sql Server Management Studio

I have a web site developed locally with a local Sql Server database. I also have a web host that provides one Sql Server database for my site. Now I want to deploy the application, and I would like to be able to manage the remote database from the Server Explorer in Visual Studio. I have the connection string used in the application, which works fine for adding, say, a datasource to a control etc. But I don't know if there's any way to use it to connect the database inside the Server Explorer so that I can add tables etc. I have read that you're supposed to be able to this instead of using the Sql Server Management Studio, but I have'nt read anything about how to connect to the remote database in it.
What I have tried so far is this: I have selected Add database in Server Explorer. This brings up first a dialog where I choose Sql Server. And then I get a dialog where I can set Server name (which I tried using the ip address in the connection string below), and Authentication (where I chose Sql Server Authentication, with the user id and password from below). But when I test the connection it fails.
Here's the connection string, which works fine when used for datasources in the application (obviously with different user name and password):
Any help appreciated!
EDIT:
Well, I've done everything suggested by lewiguez below now, but it doesn't make any difference. I can't believe this should be so hard... I keep getting this error message in Sql Server Management Studio:
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: 53)
Here's the process I follow for working with remote databases:
In VS 2008 under the Server
Explorer tab, right-click on "Data
Connections" and then click "Add
Connection"
Change the Data Source to
"Microsoft Sql Server"
Put in your fully-qualified
"Server Name" (be it IP Address, domain name, etc.), select "Sql Server
Authentication" and put in "User
Name" and "Password" for the database user
Select the database you want to connect to under "Select or enter a database name"
Hit "Test Connection" to test and then "OK" to accept
You should be able to interact with the database at this point by expanding the connection and then the "Tables" folder. For example, right-click on the Tables folder and you'll be presented with an "Add Table" option in the dialog. Right-clicking an existing table will bring up the "Open Table Definition" options where you can change any columns, etc.
If you're not getting to this point (and it sounds like you're not), I'd recommend checking your Sql Server connection from whatever machine you're using.
I generally do this by going into the Management Studio and connect with whatever credentials I'm trying to use. If they don't work, make sure they are, in fact, added as a Sql Server user and a database user (they have to be both).
Also, I would check in the Sql Server Surface Area Configuration Manager. You have to allow TCP connections and you have to be set up to allow Sql Server Authentication connections as well from remote hosts. If THAT'S all set up, but you still can't connect, I'd double-check to make sure your firewall is allowing Sql Server connections. The default port is 1433.
Also, if you can connect, but then can't write to anything, double-check your table permissions. Hope this helps!
I finally understood the problem: as I suspected the settings in my own installation was not the problem, but rather it was the settings on the Sql Server installation on my web host. And in fact I found a place in the control panel on my web host where I could set an exception to my ip address so that my ip would be granted access to the port 1433 for Sql Server, which is otherwise closed by default for security reasons.
I've had no luck finding any information about this at all on the internet, which I find strange. All the information I could find had to do with changing these settings on your own local installation of Sql Server. But I'm sure there are loads of people out there who like me use a web host for deploying their web site, and then that info doesn't seem to apply. (Perhaps apart from the TCP setting, which I think must be set, and there's detailed info about that here: http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/ )
Hope this helps someone else who like me uses a web host. Now everything works fine for me at least, both in Management Studio and in Visual Studio Server Explorer.

Linq failing to connect... or is it?

I have a web-application which is reciving the following while trying to connect to a database hosted on another server.
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)
Now, there problem here is that the connection strings, and the server paths area all perfect.
The site run correctly when run via cassini, on my local machine. (Connections strings are correct.)
Other sites hosted on the same server are able to access the databse. (Server paths ara all active.)
While the error says it's been unable to connect, Activity Monitor is aware of the query, and the Server Logs show no error. (Just odd.)
Google & Bing have massively failed with this, so I guess it's time to hit the Stack!
Open the Sql Server Conifguration Manager, expand Sql Server 2005 Network Configuration, click Protocols for <Yourserverinstance>, then check TCP/IP is enabled.

Resources