วันพุธที่ 26 กันยายน พ.ศ. 2561

วิธีแก้ปัญหา การเชื่อมต่อกับ MS SQL SERVER

Trouble connecting to your database?

In most cases, some aspect of the SQL Server configuration needs to be changed. If you're able to connect to your database from another type of desktop application such as a Windows Forms or WPF application, ensure that you've enabled TCP/IP for SQL Server. You can do that in the Computer Management console.
Computer Management


Then, make sure that your SQL Server Browser service is running.
SQL Server Browser Service

Configuring remote access on a SQL Server instance

To enable remote connection on SQL Server right – click on the server and select the Properties option. In the Server Properties dialog under the Connections tab check the Allow remote connections to this server option:
Server Properties dialog - Connections tab - checking the Allow remote connections to this server option

Configure a Windows Firewall for Database Engine Access

To add a firewall exception for the 1433 port go to Programs -> Administrative Tools select the Windows Firewall with Advanced Security option and follow the steps:
Selecting the Windows Firewall with Advanced Security option
  1. In the Windows Firewall with Advanced Security dialog click on the Inbound Rules option and select the New Rule command:
    Selecting the New Rule command
  2. In the New Inbound Rule wizard select the Port option and click Next:
    Selecting the Port option in the New Inbound Rule wizard
  3. In the Protocols and Ports window specify the protocols and ports to which a rule applies. Select the TCP option, in the Specific local ports text box enter the 1433 port, and click Next:
    Configuring protocols and ports in the New Inbound rule wizard
  4. In the Action window select the Allow the connection to specify the action to be taken when a connection matches the conditions specified in the rule:
    Selecting the Allow the connection in the Action window
  5. Specify the profiles for which the rule applies in the Profile window, and click Next:
    New Inbound Rule wizard - Profile dialog
  6. In the last window specify the name of the created rule and click the Finish button:
    Specifying the name of the created rule
You can now see the created rule in the list of inbound rules:
Viewing the created rule in the list of inbound rules

Configuring remote access on a named instance of SQL Server

If you using a named instance of SQL Server there are specific things you need to do to allow clients to access the SQL database remotely. As already stated, the default instance that SQL Server listens is port 1433. For a named SQL Server instance, the ports that are used to talk to the SQL Server are by default dynamic.
When a client makes a connection to a named instance of SQL Server, it sends a SQL Server Resolution Protocol (SSRP) UDP packet to the server machine User Datagram Protocol (UDP) port 1434.
To set up remote access to a SQL Server named instance go to Start->Programs->Microsoft SQL Server 2005/2008/2012 ->Configuration Tools and select the SQL Server Configuration Manager.
In the TCP/IP Properties dialog select the IP Addresses tab and scroll down to IPAII. Set the TCP Dynamic Ports to blank and TCP Port to 1434, and restart the SQL Server<server name> service.

ref: 
  1. https://knowledgebase.apexsql.com/configure-remote-access-connect-remote-sql-server-instance-apexsql-tools/
  2. https://docs.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases
  3. https://github.com/StefanWickDev/IgniteDemos/tree/master/NorthwindDemo