How to use a getter with a nullable?
- by Desmond Lost
I am reading a bunch of queries from a database. I had an issue with the queries not closing, so I added a CommandTimeout. Now, the individual queries read from the config file each time they are run.
How would I make the code cache the int from the config file only once using a static nullable and getter.
I was thinking of doing something along the lines of:
static int? var;
get{ var = null;
if (var.HasValue)
...(i dont know how to complete the rest)
My actual code:
private object ExecuteQuery(string dbConnStr, bool fixIt)
{
object result = false;
using (SqlConnection connection = new SqlConnection(dbConnStr))
{
connection.Open();
using (SqlCommand sqlCmd = new SqlCommand())
{
AddSQLParms(sqlCmd);
sqlCmd.CommandTimeout = 30;
sqlCmd.CommandText = _cmdText;
sqlCmd.Connection = connection;
sqlCmd.CommandType = System.Data.CommandType.Text;
sqlCmd.ExecuteNonQuery();
}
connection.Close();
}
return result;
}}