检查服务器对象是否已连接

发布于 2024-10-01 12:58:39 字数 713 浏览 4 评论 0原文

我正在使用 Microsoft.SqlServer.Management.Smo 命名空间中的 Server 对象。 我如何检查它是否成功连接到服务器,我看不到与之相关的任何属性。

                ServerConnection srvConn = new ServerConnection(server);
                // Log in using SQL authentication instead of Windows authentication
                srvConn.LoginSecure = false;
                // Give the login username
                srvConn.Login = serverUserName;
                // Give the login password
                srvConn.Password = serverPassword;
                // Create a new SQL Server object using the connection we created
                SqlServer = new Server(srvConn);

我想知道我的用户名和密码的连接是否良好。

感谢您的帮助。

I am working with Server object from Microsoft.SqlServer.Management.Smo namespace.
How i can check that it connected successfully to the server i couldnt see there any property related to it .

                ServerConnection srvConn = new ServerConnection(server);
                // Log in using SQL authentication instead of Windows authentication
                srvConn.LoginSecure = false;
                // Give the login username
                srvConn.Login = serverUserName;
                // Give the login password
                srvConn.Password = serverPassword;
                // Create a new SQL Server object using the connection we created
                SqlServer = new Server(srvConn);

I want to know if connection was good with my username and password.

Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2024-10-08 12:58:39

我相信在您实际使用连接进行某些操作之前,Smo 不会连接到服务器。根据在线书籍:

SMO将自动建立一个
需要时连接,然后释放
到连接池的连接
执行完毕后
操作。

这意味着您有两个基本策略:

  1. 检索 Server.Information.Version 或另一个属性来测试连接
  2. 只需继续做您想做的事情,并捕获任何异常

我会说 2 是更好的选择,因为没有确保现在有效的连接将在几秒钟内再次工作:服务器或网络可能会出现故障,或者 DBA 可能会禁用您的登录。从一开始就处理连接失败是使您的应用程序更加健壮的好方法。

I believe that Smo doesn't connect to the server until you actually use the connection for something. According to Books Online:

SMO will automatically establish a
connection when required, and release
the connection to the connection pool
after it has finished performing
operations.

That means you have two basic strategies:

  1. Retrieve Server.Information.Version or another attribute to test the connection
  2. Just go ahead and do what you want to do, and catch any exception

I would say that 2 is the better option, because there is no guarantee that a connection that works now will work again in a few seconds: the server or network can go down or your login can be disabled by a DBA. Handling connection failures from the beginning would be a good way to make your application more robust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文