string strConn = "Data Source=(local);Initial Catalog=pubs;Integrated Security=SSPI;"; SqlConnection conn = new SqlConnection(strConn); conn.Open(); // Do something here conn.Close();
public void ConnectSample() { string strConn = "Data Source=(local);Initial Catalog=pubs;Integrated Security=SSPI;"; SqlConnection conn = null; try { conn = new SqlConnection(strConn); conn.Open(); // Do something here } catch (Exception ex) { throw; } finally { if (conn != null) { conn.Close(); } } }