ADO.NET Connection Connection Objects, Connection Strings, and C# Examples Prepared by Rachit Joshi 1. Introduction ADO.NET is a data - access technology used by .NET applications to communicate with databases and other data sources. A connection object establishes and manages the communication channel between an application and a data source. For example, an application may use ADO.NET to connect to Microsoft SQL Server, execute a query, and read the returned records. This guide introduces connection objects, connection strings, opening and closing connections, common errors, and safe coding practices. The examples use C# and SQL Server. Exact APIs can vary by .NET version and database provider. 2. What Is an ADO.NET Connection? An ADO.NET connection is an object that represents a session with a data source. For SQL Server, the common class is SqlConnection. Other providers offer corresponding classes, such as OleDbConnection and OdbcConnection. The connection object provides methods and properties for managing connection state and related operations. • Open(): establishes a connection using the configured connection string. • Close(): closes the connection and normally returns it to the connection pool when pooling is enabled. • Dispose(): releases the connection object's resources; a using statement is a convenient way to ensure disposal. • State: reports whether the connection is open, closed, connecting, or in another state. • ConnectionString: specifies the server, database, and authentication settings required by the provider. 3. What Is a Connection String? A connection string is a sequence of settings that tells the provider how to connect to a data source. A SQL Server connection string may specify the server or instance, database name, authentication method, and other options. string connectionString = "Server=YOUR_SERVER;Database=YOUR_DATABASE;Integrated Security=True;TrustServerCertificate=True;"; Replace YOUR_SERVER and YOUR_DATABASE with the correct values for your development environment. The trust - certificate option above is shown only as an example for a development setup; production systems should use an appropriately configured and validated TLS certificate. If using username/password authentication, keep credentials out of source code and store them in a secure configuration or secrets manager. 4. How to Create and Open a Connection 1. Create a .NET project and ensure the appropriate SQL Server data provider is referenced. 2. Import the namespace used by the provider. For classic .NET Framework projects this may be System.Data.SqlClient; newer projects often use Microsoft.Data.SqlClient. 3. Prepare a valid connection string for the database you are authorized to access. 4. Create a connection object with the connection string. 5. Open the connection when database work is required. 6. Perform the required operation, handle exceptions, and dispose of the connection. Example using Microsoft.Data.SqlClient (install/reference the provider package as required by your project): using Microsoft.Data.SqlClient; \ n \ nstring connectionString = \ n "Server=YOUR_SERVER;Database=YOUR_DATABASE;Integrated Security=True;"; \ n \ nusing (SqlConnection connection = new SqlConnection(connectionString)) \ n{ \ n try \ n { \ n connection.Open(); \ n Console.WriteLine("Database connection opened successfully."); \ n } \ n catch (SqlException ex) \ n { \ n Console.WriteLine("Could not connect to the database: " + ex.Message); \ n } \ n} The using statement ensures that the connection is disposed of when execution leaves the block. Use a real server name and database configured for your environment; the placeholder values above are not executable as - is. 5. Executing a Query Through a Connection After opening the connection, an application can associate a command object with it. For SELECT statements, ExecuteReader() returns a data reader. Parameterized queries should be used whenever values come from users or other external input. using Microsoft.Data.SqlClient; \ n \ nstring connectionString = \ n "Server=YOUR_SERVER;Database=YOUR_DATABASE;Integrated Security=True;"; \ nstring query = "SELECT Name FROM dbo.ExampleTable WHERE Id = @id"; \ nint id = 1; \ n \ nusing (SqlConnection connection = new SqlConnection(connectionString)) \ nusing (SqlCommand command = new SqlCommand(query, connection)) \ n{ \ n command.Parameters.AddWithValue("@id", id); \ n connection.Open(); \ n \ n using (SqlDataReader reader = command.ExecuteReader()) \ n { \ n wh ile (reader.Read()) \ n { \ n Console.WriteLine(reader["Name"]); \ n } \ n } \ n} This example assumes that dbo.ExampleTable exists and contains Id and Name columns. Adapt the table and column names to your own schema. 6. Common Connection Classes Class Provider / use Notes SqlConnection SQL Server Use the SQL Server provider supported by your project, such as Microsoft.Data.SqlClient. OleDbConnection OLE DB data sources Requires a compatible OLE DB provider and suitable connection string. OdbcConnection ODBC data sources Uses an installed and configured ODBC driver. 7. Important Connection Properties and Methods Member Purpose ConnectionString Contains provider - specific settings used to connect. State Indicates the current state of the connection. Open() Attempts to establish a connection. Close() Closes the connection. Dispose() Releases resources owned by the connection object. BeginTransaction() Starts a database transaction associated with the connection. 8. Connection Pooling Connection pooling allows compatible database connections to be reused rather than creating a brand - new physical connection for every operation. With supported providers and default pooling settings, closing or disposing of a connection usually returns it to the pool for reuse. This can improve performance and reduce the overhead of repeatedly establishing connections. • Dispose of connection objects promptly, even when pooling is enabled. • Avoid keeping connections open when no database operation is taking place. • Connection pools are typically separated by connection - string and identity - related settings, so unnecessary variations can reduce reuse. 9. Common Errors and Troubleshooting Invalid connection string Check spelling, separators, server name, database name, and provider - specific keywords. Server not found or connection timeout Confirm the database server is running, the hostname and port are correct, network access is permitted, and the server accepts connections from your development environment. Login failed Verify that the chosen authentication method is enabled and that the account has appropriate permissions. Do not share passwords or place them in public repositories. Database does not exist Check the database name and confirm that the account can access it. Certificate or TLS error Use a valid certificate and the recommended TLS settings for the environment. Avoid disabling certificate validation as a production workaround. 10. Best Practices • Use using statements or equivalent disposal patterns to release resources reliably. • Keep connection strings and credentials in secure configuration rather than hard - coding secrets. • Use parameterized SQL commands instead of concatenating untrusted input into query strings. • Open connections only when needed and close/dispose them promptly. • Grant database accounts only the permissions they need. • Handle database exceptions without exposing credentials or sensitive connection details to end users. • Use encrypted connections and properly validate server certificates in production. • Test connection behavior in a development environment before deploying. 1 1 . Conclusion ADO.NET connections are a foundation of database access in .NET applications. By choosing the appropriate provider, configuring the connection string, opening connections only when needed, using parameterized commands, and disposing of resources reliably, developers can create more secure and maintainable data - driven applications.