In this sentence:
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
does it close connection in case of exception?
The safest way to do a "normal" query is
using (var conn = new SqlConnection("..."))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "...";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// ...
}
}
}
}
Exceptions can be caught outside this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With