Windows Azure Website connect external SQL server - c#

I just upload my project to Windows Azure website free version.
My website need to connect to external SQL server on another web hosting.(not SQL server on windows azure).
When i develop on my PC. Everything works well (that mean my connection string is correct).
But when i upload to windows azure. It can't connect to that database.
And throw exception like this.
Access is denied
when i do the following this answer, error show like this
Connecting to remote SQL Server 2008 from Windows Azure
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
and both show stack trace like this
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.
[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.
how can i solve this issue, please help.

You're uploading your application to a Windows Azure Website. Most probably port 1433 that connects to SQL Server is blocked by default on their firewall. Since you're using an azure website you will have no control over this firewall setting.
You can either:
Migrate your existing SQL Server database into SQL Azure and consume it from there
Upgrade your application to a Cloud Service where you can define the endpoints you need to connect to the external database (which I wouldn't recommend anyway because it would be very slow on deployment)
Find a way to publish your existing database as web services so you can consume them from your application, but you would probably have to rewrite your app's data access layer.
I would simply move everything to SQL Azure, it's pretty straightforward.

If you can add a website to the remote host where the database is hosted, can you create a service (e.g. WCF or Rest) on that same host that allows you to talk to the database? That way you can still host the database on the external host and communicate with it via the service from the Azure Website.

Looks like there's a new way to allow Azure webapps to connect to on premises SQL Servers using Hybrid Connections. Google leads me to believe that this feature became available sometime in 2016. It will require that you install additional software on the machine that hosts your SQL Server instance.
More information available here.

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

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

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.

.Net - Remote BizTalk server monitoring using WMI when the SQL server is located on another VM

I have created a windows service to monitor receive location and send port events i.e. start/stop/enabled/disabled/enlisted/unenlisted for a local BizTalk server machine using a WMI ManagementScope class scope:
string strScope = #"\\.\root\MicrosoftBizTalkServer";
But when I try pointing at a remote machine/server to capture events using:
string strScope = string.Format(#"\\{0}\root\MicrosoftBizTalkServer", node.Address);
I receive an odd WMI error. The typical remote machine I am trying to point towards is a similar windows server with one small distinction that its BizTalk server is pointing to a SQL server located on yet another server within the network.
After a lot of googling and searching, I came across information pointing at a possible cause of failure being the classic "Double-hop" issue which occurs when we try to access remote BizTalk server when its SQL server is on another machine.
Within visual studio I was not getting enough information on the error that I tried a windows built-in tool "wbemtest.exe" to troubleshoot what was going on only to uncover a long error message such as this:
BizTalk Server cannot access SQL server. This could be due to one of the following reasons:
\n1. Access permissions have been denied to the current user. Either log on as a user that has been granted permissions to SQL and try again, or grant the current user permission to access SQL Server.
\n2. The SQL Server does not exist or an invalid database name has been specified. Check the name entered for the SQL Server and database to make sure they are correct as provided during SQL Server installation.
\n3. The SQL Server exists, but is not currently running. Use the Windows Service Control Manager or SQL Enterprise Manager to start SQL Server, and try again.
\n4. A SQL database file with the same name as the specified database already exists in the Microsoft SQL Server data folder.
\n
\nInternal error from OLEDB provider: \"Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.\""
I have tried playing with Firewall and DCOM setting under Administrative Services -> Component Services but no setting is working for me to pass on my user's credentials via "impersonation" and retrieve back receive location / send ports information.
Please share a workaround/solution/configuration settings that actually addresses this issue.
After a lot of research and thorough study, the solution was achieved via enabling / configuring Kerberos authentication for the non-domain admin. service account user and machines involved in communication i.e. client (machine sending WMI based request), Biztalk node (machine responding to the WMI based request) and SQL Server node (machine hosting / running SQL Server database engine service and most importantly Biztalk's databases which include messagebox, tracking, management and SSO).
This was all done under a single domain and delegation was setup for both the Biztalk node and SQL Server node along with the service account used.
DCOM permissions were granted to the service account for WMI and DTC(Distributed Transaction Coordinator) permissions were setup.
Appropriate SPNs were generated and appropriate SQL Server permissions were granted to the service account initiating WMI request in delegation mode.
Verified that Kerberos was enabled for the particular service account using the following SQL query:
select session_id,net_transport,client_net_address,auth_scheme from sys.dm_exec_connections
This is a double hop issue only, the problem you are facing is because you are trying to run a service and access remote server BizTalk server, which is then trying to logon to a remote SQL server. Your credential only reaches to BizTalk server but BizTalk server does not pass your credentials to another server.
Refer to this link for solution
Similar Issue

Remote Sqlserver Connected to windows application using C#

I want to create a windows application in C# and SQL Server, these have two databases one is in local another one in remote. I have 2 doubts:
Is possible to connect remote SQL Server in windows application?
Some time Internet may be not available (may be chance). This time I cannot fetch and store data in remote server, my application not running that time,how can avoid this situation?
Full data available remote server, partial database only in local SQL Server
please share ideas to me.
is possible to connect remote sql server in windows application?
Yes. Provided if remote connection in SQL Server is enabled. See How to enable remote connections in SQL
Some time Internet may be not available (may be chance).This time I
cannot fetch and store data in remote server , my application not
running that time,how can avoid this situation?
Check for connection to your server or catch exception to see if server is unavailable. Write the data on your local server or local XML file and then implement sync logic when ever your server becomes available

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.

Categories