Click to Rate and Give Feedback
TechNet
TechNet Library
SQL Server
SQL Server 2008
Database Engine
Development
Developer's Guide
 Disconnecting from an Instance of S...
Community Content
In this section
Statistics Annotations (0)
Collapse All/Expand All Collapse All
Other versions are also available for the following:
SQL Server 2008 Books Online (October 2009)
Disconnecting from an Instance of SQL Server

Manually closing and disconnecting SQL Server Management Objects (SMO) objects is not required. Connections are opened and closed as required.

When the Connect method is called, the connection is not automatically released. The Disconnect 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 NonPooledConnection property of the ConnectionContext property that references the 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 ServerConnection object, this object is also used when disconnecting from an instance of Microsoft SQL Server when you program by using RMO. To close a connection by using the ServerConnection object, call the Disconnect method of the RMO object. After the connection has been closed, RMO objects cannot be used.

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see "How to: Create a Visual Basic SMO Project in Visual Studio .NET" or "How to: Create a Visual C# SMO Project in Visual Studio .NET" in SQL Server Books Online.

This code example shows how to request a non-pooled connection by setting the NonPooledConnection property of the ConnectionContext object property.

VisualBasic
Dim srv As Server
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()

This code example shows how to request a non-pooled connection by setting the NonPooledConnection property of the ConnectionContext 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();
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker