我有一個 C# 應用程式,當我在 Visual Studio 中測驗我的應用程式時,它連接到位于我機器上的在線服務器它作業正常,但是當我將應用程式提供給客戶端時它不起作用我直接在我的代碼中使用這個連接字串在單擊按鈕事件中:
@"Data Source =MyServerIp\SQLEXPRESS; Initial Catalog = MyDBname; User Id = gues; Password=gues";
我已經在我的 SQL 服務器中打開了混合模式,并且我已經在我的防火墻中正確完成了配置,并且在我的路由器埠轉發中仍然出現了該錯誤:
您的應用程式中發生了未處理的例外。如果單擊繼續,應用程式將忽略錯誤并嘗試繼續。如果單擊退出,應用程式將立即關閉。建立與 SQL Server 的連接時發生與網路相關或特定于實體的錯誤。服務器未找到或無法訪問。驗證實體名稱是否正確以及 SQL Server 是否配置為允許遠程連接。(提供者:命名管道提供者,錯誤:40 - 無法打開與 SQL Server 的連接)。
我確定我的 IP 并且可以通過 Internet 訪問它,而且我確定我的憑據和防火墻設定
有人告訴我客戶端必須安裝本地資料庫我試過了,我安裝了本地資料庫但沒有配置任何東西
你能幫我找出我的問題到底在哪里嗎?
更新 (問題已解決) 我可能在 onc 遇到了兩個問題,這使得除錯變得更加困難。我的兩個問題是:
客戶端機器上過時的網路框架。
錯誤的sql連接字串,它得到了下面的答案。
對不起,我不知道如何將問題狀態更改為已解決..
uj5u.com熱心網友回復:
從以下開始,解決您無法連接到 SQL Server 的原因。
打開 PowerShell 并運行以下命令來檢查 SQL Server 是否正在偵聽 TCP/IP 埠:
注意:命令來自這篇
如果沒有,它不會回傳任何東西。在這種情況下,繼續下一步。
打開
在 SQL Server 配置管理器中,確保為 SQL Server 啟用 TCP/IP。如果沒有,請啟用它。

雙擊 TCP/IP 打開屬性視窗。單擊“IP 地址”選項卡。滾動到底部。您可以在此處將其從使用“TCP 動態埠”更改為使用指定的 TCP 埠(即: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-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
- Visit the following URL which will show 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
- Resolving connectivity errors to SQL Server
- Troubleshoot connecting to the SQL Server Database Engine
- Not listening SQL Server Port 1433
- SQL Server Configuration Manager
- Network Protocols and Network Libraries
- Default SQL Server Network Protocol Configuration
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430436.html
