我正在使用MVC快速尝试Stack Overflow/Sam Saffron发布的新Dapper Micro ORM。我想知道在我的控制器内管理SQLConnection对象的最简单方法是什么?我只是为了快速浏览一些数据并测试Dapper而做了一些简单的事情,但像这样打开/关闭连接是否理想?
public class HomeController : Controller
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ajh"].ConnectionString);
public HomeController()
{
}
public ActionResult Index()
{
// get me all comments
conn.Open();
var comments = conn.ExecuteMapperQuery<Comment>("select * from Comment");
conn.Close();
return View(comments);
}
}