Assembly: System.Data (in system.data.dll)
The connection-level error that occurred while opening the connection.
The Close method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled.
Pending transactions started using Transact-SQL or BeginTransaction are automatically rolled back. Transactions started through System.Transactions are controlled through the System.Transactions infrastructure, and are not affected by SqlConnection.Close.
An application can call Close more than one time. No exception is generated.
If the SqlConnection goes out of scope, it is not closed. Therefore, unless you have placed your code inside of a using statement, you must explicitly close the connection by calling Close or Dispose. They are functionally equivalent. If the connection pooling value Pooling is set to true or yes, this also releases the physical connection.
Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, you should only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. For more information, see Garbage Collection.
The following example creates a SqlConnection, opens it, displays some of its properties. The connection is automatically closed at the end of the using block.
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.