| title | Disconnecting from an Instance of SQL Server | Microsoft Docs | ||||
|---|---|---|---|---|---|
| ms.custom | |||||
| ms.date | 06/13/2017 | ||||
| ms.prod | sql-server-2014 | ||||
| ms.reviewer | |||||
| ms.technology | |||||
| ms.topic | reference | ||||
| helpviewer_keywords |
|
||||
| ms.assetid | 4ca7f7eb-6b3f-4c73-ac63-88afa8570b61 | ||||
| author | stevestein | ||||
| ms.author | sstein | ||||
| manager | craigg |
Manually closing and disconnecting [!INCLUDEssNoVersion] Management Objects (SMO) objects is not required. Connections are opened and closed as required.
When the xref:Microsoft.SqlServer.Management.Common.ConnectionManager.Connect%2A method is called, the connection is not automatically released. The xref:Microsoft.SqlServer.Management.Common.ConnectionManager.Disconnect%2A method must be called explicitly to release the connection to the connection pool. Also, you can request a non-pooled connection. You do this by setting the xref:Microsoft.SqlServer.Management.Common.ConnectionSettings.NonPooledConnection%2A property of the xref:Microsoft.SqlServer.Management.Smo.Server.ConnectionContext%2A property that references the xref:Microsoft.SqlServer.Management.Common.ServerConnection object.
Closing server connections when you are programming with RMO works slightly different from SMO.
Because the server connection for an RMO object is maintained by the xref:Microsoft.SqlServer.Management.Common.ServerConnection object, this object is also used when disconnecting from an instance of [!INCLUDEmsCoName] [!INCLUDEssNoVersion] when you program by using RMO. To close a connection by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection object, call the xref:Microsoft.SqlServer.Management.Common.ConnectionManager.Disconnect%2A method of the RMO object. After the connection has been closed, RMO objects cannot be used.
[!INCLUDEssChooseProgEnv]
This code example shows how to request a non-pooled connection by setting the xref:Microsoft.SqlServer.Management.Common.ConnectionSettings.NonPooledConnection%2A property of the xref:Microsoft.SqlServer.Management.Smo.Server.ConnectionContext%2A object property.
This code example shows how to request a non-pooled connection by setting the xref:Microsoft.SqlServer.Management.Common.ConnectionSettings.NonPooledConnection%2A property of the xref:Microsoft.SqlServer.Management.Smo.Server.ConnectionContext%2A object property.
{
Server srv;
srv = new Server();
//Disable automatic disconnection.
srv.ConnectionContext.AutoDisconnectMode = AutoDisconnectMode.NoAutoDisconnect;
//Connect to the local, default instance of SQL Server.
srv.ConnectionContext.Connect();
//The actual connection is made when a property is retrieved.
Console.WriteLine(srv.Information.Version);
//Disconnect explicitly.
srv.ConnectionContext.Disconnect();
}
xref:Microsoft.SqlServer.Management.Smo.Server
xref:Microsoft.SqlServer.Management.Common.ServerConnection