Home > Software engineering >  Can't access my SQL Server via C# form application from client device
Can't access my SQL Server via C# form application from client device

Time:02-21

I have a C# application that connect to an online server located on my machine when I test my application in visual studio it works fine but when I give the application to the client it doesn't work I am using this connecting string in my code directly in a click button event:

@"Data Source =MyServerIp\SQLEXPRESS; Initial Catalog = MyDBname; User Id = gues; Password=gues";

I have turned on mixed mode in my SQL server and I have done the configuration properly in my firewall and also in my router port forwarding still getting that error:

unhandled exception has occurred in your application. if you click Continue, the application will ignore the error and attempt to continue. If you click Quit, the application will close immediately. A network-related or instance-specific error occurred while establishing a connecting 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).

I am sure of my IP and that it is accessible via the internet and also I am sure from my credential and my firewall settings

I have been told that the client must install a Local DB I have tried that, I installed Local DB I but didn't configure anything

Can you help me to find where is my problem exactly please?

CodePudding user response:

Start with the following to troubleshoot why you're not able to connect to your SQL Server.

Open PowerShell and run the following to check if SQL Server is listening on a TCP/IP port:

Note: The command is from this enter image description here

If not, it won't return anything. In this case proceed to the next step.

Open enter image description here

In SQL Server Configuration Manager, ensure TCP/IP is enabled for SQL Server. If not, enable it.

enter image description here

Double-click TCP/IP to open the properties window. Click on the "IP Addresses" tab. Scroll to the bottom. This is where you can change it from using "TCP Dynamic Ports" to using a specified TCP Port (ie: 1433)

enter image description here

If you've made any changes to the TCP Port, you'll have to restart the SQL Server services (Control Panel => Administrative Tools => Services => SQL Server...). Alternatively, you can restart your computer.

Get SQL Server instance name

  • Open PowerShell, and type the following:
Get-WmiObject -Namespace Root\Microsoft\SqlServer  -Query "Select Name from __Namespace where Name like 'ComputerManagement%'" | ForEach-Object { $sqlMgmtVer = $_.Name; Get-WmiObject -Namespace Root\Microsoft\SqlServer\$sqlMgmtVer -Class FileStreamSettings |Select-Object InstanceName }

Identify your local IP Address

  • Open PowerShell, and type the following:
Get-WMIObject -Namespace Root\cimv2 -Query "SELECT Description, DHCPEnabled, DHCPServer, IPAddress, MACAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True" | Select-Object Description, DHCPEnabled, DHCPServer, IPAddress, MACAddress

Identify your public IP Address

Configure the Windows Firewall to Allow SQL Server Access.

Configure a Windows Firewall for Database Engine Access (shows how to configure the Windows firewall when using dynamic ports also)

Note: In addition to configuring the Windows firewall, it may be necessary to set up Port Forwarding in your router. Refer to the documentation for your router for more information.

Additional Resources

CodePudding user response:

Thank you so much for your replay sir I have done everything you said

my SQL Server is listening to ports and my Instance name is correct

and my public IP is correct too, I check it every time when I want to test my app, because it is not static

I have also configured my widows firewall and I also tried to shut it down, and also I configured my port forwarding in my router

Still nothing fixed my problem sadly

There is something I would like to add, in another client machine that does have an SSMS and a SQL Server, that machine could use my application perfectly without any problems I wonder if there is any requirements that should exist in the client machine if I want to use this method to connect to my Server via a C# Form application

@user9938

  • Related