This documentation is archived and is not being maintained.

SqlConnection.Dispose Method

.NET Framework 1.1

Releases the resources used by the SqlConnection.

Overload List

Releases the unmanaged resources used by the SqlConnection and optionally releases the managed resources.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Protected Sub Dispose(Boolean)
[C#] protected override void Dispose(bool);
[C++] protected: void Dispose(bool);
[JScript] protected override function Dispose(Boolean);

Inherited from Component.

[Visual Basic] Overloads Public Overridable Sub Dispose() Implements IDisposable.Dispose
[C#] public virtual void Dispose();
[C++] public: virtual void Dispose();
[JScript] public function Dispose();


[Visual Basic, C#, C++] The following example creates a SqlConnection and then disposes of it.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Dispose. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub SqlConnectionHereAndGone()
    Dim myConnection As New _
       SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;")
    'Calling Dispose also calls SqlConnection.Close.
End Sub

public void SqlConnectionHereAndGone() 
   SqlConnection myConnection = new 
        SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
   //Calling Dispose also calls SqlConnection.Close.

void SqlConnectionHereAndGone() 
   SqlConnection* myConnection = new 
        SqlConnection(S"Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
   //Calling Dispose also calls SqlConnection.Close.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

SqlConnection Class | SqlConnection Members | System.Data.SqlClient Namespace