【发布时间】:2011-04-19 13:31:40
【问题描述】:
我正在使用 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);
}
}
【问题讨论】:
标签: c# asp.net-mvc orm sqlconnection