WebMatrix Database. Open ... Close () and Dispose ()

Anytime I read about Close () and Dispose (), I get a lot of links to just use the "Use Block", but I have not yet been able to find how to use the "Use Block in C # Syntax Syntax" WebMatrix syntax.

So I don’t need an answer that says that just use the Use block if you can’t tell me exactly how with the example.

Specific to use Database.Open () after I finished with my connection / queries

My questions:

  • Should I use both Close () and Dispose ()?
  • Does it matter if I call Close () and then Dispose () or Dispose () and then Close ()?

We hope for simple answers to simple questions. Thanks

+4
source share
1 answer

The ASP.NET web page structure examples for the database assistant do not include Close or Dispose calls, since the framework itself is designed to call Dispose for you at the end of the query. If you use ADO.NET instead of the database helper, you must use the instructions. Having said that, you have nothing to stop wrapping database calls with blocks:

IEnumerable<dynamic> floaters = null; using(var db = Database.Open("MyDb")){ var sql = "SELECT * From LifeRafts"; floaters = db.Query(sql); } 

If you want to manage all of this, you can simply call Close or Dispose. Both of them cause the connection to still return to the ADO.NET connection pool.

+4
source

Source: https://habr.com/ru/post/1442845/


All Articles