Checking that Server object connected
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:
- Retrieve Server.Information.Version or another attribute to test the connection
- 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.
精彩评论