I'm trying to execute a SQL query like following, but Visual Studio complains a CA2000.
public static IDictionary<string, string> TemplateDirectories(string connectionString) {
var directories = new Dictionary<string, string>();
using (var connection = new SqlConnection(connectionString)) {
connection.Open();
using (var command = new SqlCommand { Connection = connection, CommandText = "select * from PATHS" }) {
var reader = command.ExecuteReader();
while (reader.Read())
directories[reader["CODE"].ToString()] = reader["PATH"].ToString();
reader.Close();
}
}
return directories;
}
Error CA2000 ...object 'new SqlCommand()' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'new SqlCommand()' before all references to it are out of scope.
I tried several ways to fix it, but no one worked. So how to fix?