How to use a getter with a nullable?
Posted
by
Desmond Lost
on Stack Overflow
See other posts from Stack Overflow
or by Desmond Lost
Published on 2013-11-11T23:42:09Z
Indexed on
2013/11/12
21:54 UTC
Read the original article
Hit count: 282
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;
}}
© Stack Overflow or respective owner