Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I'm currently connecting to the database using the following method:
using (var db = new Database("MyConnectionString"))
All the examples are shown like this, including those used for the NPoco fork of PetaPoco examples. This is generally accepted as being the best practice approach. It certainly looks neat and tidy - more so than the other approach:
var db = new Database("MyConnectionString");
var myObject= db.SingleById<MyType>(1234);
However, according to Microsoft, this is bad. Why? Because if your code throws an exception before the closing bracket, the Dispose() method is not called (since this is the place where any remaining connections are closed) and I was wondering if someone could clarify what the best practice way is of using PetaPoco to ensure correct handling of the database connection? If for example I wrapped the above two examples in try - catch - finally blocks, with the second one I could at least dispose of the database for sure by manually calling the Dispose(). Thanks in advance!
Re-read the MS docs carefully. In a using statement, the Dispose is in a finally block and is always called. So the first syntax is the preferred one, yes.
A quick look into the source code at https://github.com/CollaboratingPlatypus/PetaPoco/blob/development/PetaPoco/Database.cs shows that a using statement is already being implemented. There is no need to apply the using statement in your code.
Go to this site you will find complete information regarding
how to use peta poco tables with Umbraco
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted