WPF .NET App Cannot Connect to SQL Server on Different Network - c#

My WPF application cannot connect to an external SQL Server. The host machine has port 1433 open, has a static IP address, and port forwarding from the IP address to host server has been enabled.
Most puzzling, I am able to connect from SQL Server Management Studio on the client machine using the same connection name, and same server authentication details, but I cannot connect with my .NET application. I am using Entity Framework, which is working until the app is built and queries the server.
Therefore, I have ruled out server hosting issues, and connection string issues, as I'm able to connect while working on Visual Studio, but cannot connect while app is running.
The error (inner exception) is
TCP provider, error 0 - Access is Denied

if you are able to connect through SSMS remotely to sql server there should not be problem for connecting through application using ado.net, you need to recheck your ef configuration and connection string
I assume you are using entity framework 6.
Based on the approach like code first based or designer model based you need to validate your configuration
Please refer to below link
https://learn.microsoft.com/en-us/ef/ef6/fundamentals/configuring/connection-strings
If you are using ef core refer to below link and validate your configuration for connection string
https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

Related

Azure SQL DB connection issues from C#

I'm having trouble connecting with my .NET 5 web app to an Azure SQL DB.
The weird thing is that I can connect to the DB using Azure Data Studio.
I've configured the Azure SQL Server firewall to accept connections from my IP address, and all of the Inbound/Outbound IP addresses of my Azure App Service.
Before adding my local IP address to the firewall, I could not connect with Azure Data Studio, so that works as intended, but then I use the connection string provided by Azure itself (in ADO.NET format, since I'm using Entity Framework) inside my web app, and the app can't connect to the DB (it times out with 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)).
I can't really understand why my localhost instance can't connect, as the IP address trying to connect to the db is the same as when I use Azure Data Studio!
And also the App Service instance in Azure can't connect either (in the SQL Server's firewall I've also enabled connectivity from Azure App Services!!).
The connection string is of the form
Server=tcp:[my server].database.windows.net;Initial Catalog=[my db];Persist Security Info=False;User ID=[my username];Password=[my password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Any hints?
EDIT:
to recap as requested,
dotnet run locally doesn't connect
containerized app service doesn't connect
Azure Data Studio locally does connect
I've found out the issue in the meantime, but I don't understand the cause:
dotnet run from WSL doesn't work; if I run the app from Windows it does work!!
I've therefore changed the App Service to build from source instead of running a containerized image, and it also does work.
Why would running the app locally from WSL instead of Windows result in the Azure SQL Server's firewall blocking me?! Is WSL exposed to the internet with a different IP address?
And why does the same happen to an Azure App Service that runs the containerized version of my app?
Since you can access the server from the same machine, it is not a firewall issue.
There are two possibilities you could check:
The connection string that your program is using is not what you expect. Try logging the connection string.
The server is configured to use named pipes and not tcp. Azure Data Studio is configured to use named pipes and therefore works. Try checking the server configuration.

can't connect to a database remotely on a c# app which i can see by visiting that server's phpmyadmin site

I have added a database via phpmyadmin on a server. I can access it via http://ipaddress/phpmyadmin . but when i try using those credentials to connect to that db on a c# app , i get the error : unable to retrieve the list of database.
I don't have the right to access the server. I have a user who has rights to two db which i can see when i remotely connect to that server's phpmyadmin. Is it possible that i have right to the db just via phpmyadmin but can't access it via app because i don't have the right to do it because mysql is forbidding it ? I also have a raspberry pi handy. is using it as a db server a better alternative than this ?
Since phpMyAdmin runs on a web server, most people install it on the same machine the database is running on and access it remotely through the network. That means their phpMyAdmin is communicating with MySQL locally, often without the connection leaving the server. Your C# application, on the other hand, is a compiled application that would likely run on your client machine, not the server - meaning any connection from the application to the database is occurring through the network (or internet, depending again on your configuration). Since exposing the MySQL port is generally a bad idea, your MySQL may be configured to only listen locally, or you may have a firewall or NAT connection blocking you from even reaching the server. If that's the case, you may need to rethink how you'll communicate with the database (oftentimes exposing an API is a better solution than opening port 3306 to the world).
It's also possible you're using two different user accounts. To MySQL, an individual user account consists not only of the username, but also the hostname. You can use a wildcard hostname (%) which means all hosts, except it's really "all hosts connecting via TCP/IP networking connection."
It's possible that your application and your phpMyAdmin are using two different connection methods; one using TCP/IP and the other using sockets. Check which phpMyAdmin is using (the easiest way to do so is to look at the host phpMyAdmin is connecting to; 'localhost' is a socket connection and '127.0.0.1' is networking). Configure your C# application the same way.

Entity Framework cannot connect to Database via Azure Hybrid connection

I'm currently working on an API, to access data from a SQL Server that is hosted locally. My app is being hosted through an Azure web app. I'm also using Entity Framework 6 in my application to access data. Since my data can't be in the cloud, I created an Azure Hybrid connection to create a relay to my SQL Server.
However, I keep running into this error (see below). I don't understand why this happens. I have checked my firewall rules to make sure that connections from Azure are being permitted. I have also verified that my connection string is correct and that I'm using the right port number. I also verified that remote connections are allowed on the SQL Server.
In case anybody is wondering I'm running SQL Server 2012. Does anyone have a fix for this issue? I would really appreciate it.
Thanks in advance.
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.

Unable to connect in SQL Management Studio

My application is throwing error from a client machine of not able to connect to the database. So as a test I use SQL Management Studio 2008 on that same client machine to try and connect to the database and I found out that if I use IP\InstanceName then I can connect but if I use ServerComputerName\InstanceName then I can't connect. I have verified the following:
The server has the same gateway and subnet mask as the client.
I can ping the server using either its IP address or its computer name.
No Firewall on either server or client machine.
Verify Server SQL browser service is running.
Verify Server is set to allow remote connection.
Verify Server TCP/IP is enabled via SQL SErver Configration Manager.
I can connect from a different client machine using ServerComputerName\InstanceName
Both server and client are using static IP setup with no DNS server under IPV4 properties.
Tracert from client to server shows only one hop, that is directly to the server machine IP.
I can remote access the client machine from the server machine or viceversa using host name.
I then go to the server machine and connect using LNKLAB8\Xmark (LNKLAB8 is the host name and Xmark is my instance name) and I connected successfully. However, when I run the command SELECT ##SERVERNAME, the result I got back is LNKLABARIAL8\XMARK
This baffled me, because if I right click on the database properties as per the screenshot below, it shows LNKLAB8/Xmark .. am I going crazy or something weird going on here? after taking at the steps I've taken, do you have any suggestion of how I can get the client machine to connect to the database using the server host name instead of the server IP address?
I've kind of hit the wall on this issue, I'm unsure if I am facing a network related issue or it is a database setting issue.
Edit: When I tried to connect using host name to the server DB I would get the following:
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)
Try add servers IP and Name to c:\WINDOWS\system32\drivers\etc\hosts
1) Make sure you've enabled TCP/IP (Configuration manager), and restarted MSSQL
<= it sounds like you've done this.
2) Make sure you can "ping" by hostname and by IP
<= It sounds like you've done this, too
3) Make sure MSSQL Browser is running
4) If you're using Windows authentication (vs. SQL Mixed), then make sure you can authenticate to the remote server:
net use \\remote-server /user:<name> <password>
5) Failing all else, you might want to consider reinstalling MSSQL-related components on either/both client or server (for example, MDAC)
Links:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/4d2ce34a-508f-4a1d-8828-8806ccc12f77/sql-server-2005-cant-connect-remotely-using-host-name?forum=sqlsetupandupgrade
http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx
https://serverfault.com/questions/384700/error-connection-to-sql-server-using-hostname-instance-or-fqdn-instance-ip
'Hope that helps!
I noticed this:
8: Both server and client are using static IP setup with no DNS server under IPV4 properties.
How is name resolution happening, then? WINS? NetBIOS? Sql Server may not like that. Try checking that you have a working DNS server with the appropriate entry for your Sql Server system, and things may work better.

mysql remote server access from C# application

I have done a small C# Win Forms application, that connects to mySQL database using mySQLconnector. This works perfectly ok with my localhost.
String ConnectionString = "server=xx.xxx.xx.xxx;uid=myuserid;pwd=mypassword;database=dbname";
But I do get following error when I try to connect to remote server:
Unable to connect to any of the specified MySQL hosts.
{"No connection could be made because the target machine actively refused it xx.xxx.xx.xxx:3306"}
I have pinged the server from command prompt and I get response.
I also verified mySQL server settings in remote server database (correct name/ip, portno:3306, skip networking:OFF)
The remote server is accessible without any problem from web server that runs php script and uses same settings as the connection sting I listed above.
The mySQL server is hosted on external shared hosting company 123reg.co.uk with me logging to phpmyadmin using same uid/passwd as like in connection string.
I can't understand what is different between webserver accessing OK, but my C# app having trouble with it??
Can any one please help...
Thanks in Advance
Sam
I've had issues with servers setup so they won't accept remote connections. This is for security reasons mainly. Some places will let you enable it in the CP, but if you have direct access to the server, you should be able to follow these steps.
http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely
Your ip address will be different from the web server.
If your ip address is static you can grant access to your ip address within your mysql database ( https://www.google.com.au/search?q=mysql+grant+access+to+user+from+host ).
I'd recommend using the server to access the database and make your c# application talk to your webserver using ajax/json calls ( https://www.google.com.au/search?q=C%23+Win+Forms+ajax+json ) - that way you're not tied to an ip address for access.
Alternatively you can look at setting up ssh tunnels to your webserver if remote shell access is available so you can get the same access as your webserver. ( https://www.google.com.au/search?q=ssh+tunnels+example+mysql That's what I do myself)

Categories

Resources