开发者

Checking that Server object connected

开发者 https://www.devze.com 2023-01-24 07:01 出处:网络
I am working with Server object fromMicrosoft.SqlServer.Management.Smo namespace. How i can check that it connected successfully to the server i couldnt see there any property related to it .

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 us开发者_C百科ing 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消