In one of my previous questions someone suggested that instead of using trycatch and closing the connection at finally I should just use "using". Can someone give me an example?
I have this code, how do I use just "using" with it?
try
{
conn3.Open();
string sql_check = "SELECT (time_in) FROM timeinout WHERE employee_id = @employee_id AND date = @date";
using var cmd_check = new NpgsqlCommand(sql_check, conn3);
cmd_check.Parameters.AddWithValue("employee_id", id);
cmd_check.Parameters.AddWithValue("date", date);
cmd_check.Prepare();
var reader = cmd_check.ExecuteReader();
if (reader.Read())
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
finally
{
conn3.Close();
}