Ben*_*tch 3 c# sql getter timeout nullable
我正在从数据库中读取一堆查询。我遇到查询未关闭的问题,因此我添加了 CommandTimeout。现在,各个查询每次运行时都会从配置文件中读取。如何使用静态可为空和 getter 使代码仅缓存配置文件中的 int 一次。我正在考虑做一些类似的事情:
static int? var;
get{ var = null;
if (var.HasValue)
...(i dont know how to complete the rest)
Run Code Online (Sandbox Code Playgroud)
我的实际代码:
private object QueryMethod(string item)
{
using (SqlConnection connection = new SqlConnection(item))
{
connection.Open();
using (SqlCommand sql = new SqlCommand())
{
AddSQLParms(sql);
sql.CommandTimeout = 30;
sql.CommandText = _cmdText;
sql.Connection = connection;
sql.CommandType = System.Data.CommandType.Text;
sql.ExecuteNonQuery();
}
connection.Close();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
第一:不要调用它var!
我们就这样称呼它吧cached_value。
static int? cached_value;
get { return cached_value ?? cached_value = your_logic_here }
Run Code Online (Sandbox Code Playgroud)
这样,第一次调用它时,如果它为空,它将初始化该字段。下次调用 getter 时,您将获得所需的值。