Connection Object REPORT
Connection Object REPORT
Connection Object REPORT
1 Connection Object
Definition:
A Connection Object in .NET establishes and manages a session with a specific data source. It is
responsible for opening and closing the connection, setting the connection string, and
communicating with the data source.
State Property:
Definition: Indicates the current state of the connection (e.g., Closed, Open, Connecting,
Executing, Fetching, Broken).
Example:
Open() Method:
Definition: Opens a database connection with the settings specified in the connection string.
Example:
Close() Method:
Definition: Closes the connection to the database.
Example:
Dispose() Method:
Definition: Releases the connection object’s resources. Often used within a using statement for
automatic disposal.
Example:
ConnectionTimeout Property:
Definition: Specifies the time to wait while trying to establish a connection before terminating
the attempt and generating an error.
Example:
Types of Connection Objects
SqlConnection:
Connects to Microsoft SQL Server.
Example:
OleDbConnection:
Connects to various data sources using OLE DB.
Example:
OracleConnection:
Connects to Oracle databases.
Example:
OdbcConnection:
Connects to databases using ODBC drivers.
Example:
Best Practices
Use ‘using’ Statement: Ensures the connection is closed and resources are released automatically
even if an exception occurs.