using System.Data.SqlClient; string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"; using(SqlConnection connection = new SqlConnection(connectionString)) { // Do something with the connection connection.Open(); // ... // ... }
using System.Data.SqlClient; SqlConnection connection = new SqlConnection(connectionString); try { // Do something with the connection connection.Open(); // ... // ... } finally { connection.Dispose(); }This example demonstrates the use of the Dispose method to release the resources used by the SqlConnection object. The try-finally block ensures that the Dispose method is always called, even if an exception is thrown. The package library for this namespace is "System.Data.SqlClient".