Connection with using() block:
- Using block is used to close the connection automatically.
- We don’t need to call close () method explicitly, using block do this for ours implicitly when the code exits the block.
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); } |
Code Program:
using System; using System.Data.SqlClient; namespace Practice { class Program { static void Main(string[] args) { using(SqlConnection con = new SqlConnection(“data source=.; database=fullstack; integrated security=SSPI”)) { con.Open(); Console.WriteLine(“Connection Established Successfully”); } } } } |