简体   繁体   中英

Can't access my SQL Server via C# form application from client device

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?

UPDATE (problem solved) I probably had two problems at onc, which made it harder to debug. My two problems were:

  1. Out dated net framework at client machines.

  2. Wrong sql connection string, it got fixed by the answer below.

Excuse me I don't know how to change the state of question to Solved..

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 post

ForEach ($SQL_Proc in Get-Process | Select-Object -Property ProcessName, Id | Where-Object {$_.ProcessName -like "*SQL*"})
{
    Get-NetTCPConnection | `
     Where-Object {$_.OwningProcess -eq $SQL_Proc.id} | `
      Select-Object -Property `
                                @{Label ="Process_Name";e={$SQL_Proc.ProcessName}}, `
                                @{Label ="Local_Address";e={$_.LocalAddress + ":" + $_.LocalPort }},  `
                                @{Label ="Remote_Address";e={$_.RemoteAddress + ":" + $_.RemotePort}}, State | `
      Format-Table
} 

If it's listening, you'll see something similar to the following:

在此处输入图像描述

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

Open SQL Server Configuration Manager

在此处输入图像描述

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

在此处输入图像描述

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)

在此处输入图像描述

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-CimInstance -Namespace Root\Microsoft\SqlServer  -Query "Select Name from __Namespace where Name like 'ComputerManagement%'" | ForEach-Object { $sqlMgmtVer = $_.Name; Get-CimInstance -Namespace Root\Microsoft\SqlServer\$sqlMgmtVer -Class FileStreamSettings |Select-Object InstanceName }

Alternatively, open a cmd window and type:

sc query type=service | find /i "sql"

Identify your local IP Address

  • Open PowerShell, and type the following:
 Get-CimInstance -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.


Update

provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

According to SQL Server clients may change protocols when the client computers try to connect to an instance of SQL Server

...if a client computer has both TCP and Named Pipes available, and the order is:

  • TCP
  • Named Pipes

When the client computer tries to make a TCP connection to the server and the connection attempt returns a non-zero return code, the client transparently tries a connection by using the next protocol in the list, which is Named Pipes...

The client does not receive an error that indicates the first protocol failed.

If the client application uses the second protocol, and it also returns an error, an error is returned to the client.

If you make an alias by using one of the following methods, the client application uses the alias information to establish a connection to the server and does not use any additional protocols ... If you want to control the protocol that a client application uses for every connection attempt, and not allow the client to try multiple protocols, you can do one of the following:

Use the SQL Client Network utility or SQL Server Configuration Manager to create an alias by specifying the protocol you prefer.

Specify the protocol in your connection string.

According to the connection string documentation, to specify that the connection only use TCP/IP, Network Library=DBMSSOCN; needs to be specified in the connection string.

Example :

Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

See also Network Protocol for SQL Server Connection

According to Configure a Windows Firewall for Database Engine Access

The SQL Server Browser service lets users connect to instances of the Database Engine that are not listening on port 1433, without knowing the port number. To use SQL Server Browser, you must open UDP port 1434. To promote the most secure environment, leave the SQL Server Browser service stopped, and configure clients to connect using the port number. ... As an alternative to configuring SQL Server to listen on a fixed port and opening the port, you can list the SQL Server executable (Sqlservr.exe) as an exception to the blocked programs. Use this method when you want to continue to use dynamic ports. Only one instance of SQL Server can be accessed in this way.

To see which SQL Server services are running :

Open PowerShell and type the following:

Get-Service | Where-Object { $_.DisplayName -Match "^SQL Server.*"}

Additional Resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM