I was just writing some code the other day, and I found myself writing something I am not sure whether it is OK to do it like this, or not.
Let me give an example of what this is:
I have this Method:
public static string STOLENVersion()
{
string version = string.Empty;
try
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("SELECT TOP 1");
sb.AppendLine(" [WebsiteVersion]");
sb.AppendLine("FROM [WebsiteDefaults]");
using (SqlCommand cmd = new SqlCommand(sb.ToString(), NewSQLConnection))
{
cmd.Connection.Open();
version = cmd.ExecuteScalar().ToString();
cmd.Connection.Dispose();
}
}
catch (Exception)
{
throw;
}
return "Version: " + version;
}
And I have this property:
public static SqlConnection NewSQLConnection
{
get
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["DebugDB"].ConnectionString);
}
}
I am disposing the SqlCommand's connection inside its using block, and this method can be called multiple times (maybe like 10 times every minute, as users log on to the website, as an example)