且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过VPN连接到远程SQL Server

更新时间:2023-02-22 19:36:07

如果您提供了错误消息,那会更容易.

我建议您调试代码,看看您遇到了什么错误.

无论如何,这是要检查的几件事:

1.服务器是否允许远程连接? (可以使用SQL Server外围应用配置器进行检查和设置)
2.如果可以,是否允许通过TCP/IP连接? (因为您已经在连接字符串中提到了NetworkLibrary)
3.除去了TrustedConnection部分,因为您已经提供了用户ID和密码.
4. IP地址应随附端口号,以防您将其更改为默认端口号.


其他几件事情要考虑:

1.在该代码中尝试/捕获设置似乎无效.
2.您尝试两次关闭连接.
3.捕获块是通用的.应该使它具体化,而不是假设打开连接时代码失败.
If you had provided the error message, it would have been easier.

I would recommend to debug the code and see what error you are getting.

Anyways, here are few things to check:

1. Does the server allows remote connection? (SQL Server Surface Area Configuration can be used to check and set up)
2. If it does, does it allows to connect via TCP/IP? (Since you have mentioned that NetworkLibrary in connection string)
3. Get rid of TrustedConnection part since you have provided the user id and password.
4. IP address should be accompanied by a port number in case you have changed it from default one.


Few other things to be considered:

1. Try/Catch setup in that code does not looks effective.
2. You are trying to close the connection twice.
3. Catch blocks are generic. It should be made specific rather than assuming code failed while opening the connection.