Traktor
Traktor

Reputation: 53

how to connect to database linq

In SQL we open connection

 conn.Open();
 cmd.ExecuteNonQuery();
 conn.Close();

or

 using (conn = new SqlConnection(connectionString))
 {
     cmd1.Connection = conn;
     conn.Open();
     cmd1.ExecuteNonQuery();
 }

What is best practice for LINQ to SQL:

 DataClassesLinqDataContext dbLinq = new DataClassesLinqDataContext();

or

 using(DataClassesLinqDataContext dbLinq = new DataClassesLinqDataContext())
 {
      var x = ...
 }

Upvotes: 2

Views: 183

Answers (2)

AdaTheDev
AdaTheDev

Reputation: 147374

Yes, best practise is to dispose of the context to free up the resources it holds so I'd go with the using ()

Upvotes: 4

Pravin Pawar
Pravin Pawar

Reputation: 2569

offcourse with using "using" clause, it just ensures the dispose method gets called once it ends its scope. No need to free expensive variables like database connection.

Upvotes: 2

Related Questions