日期:2014-05-17  浏览次数:20410 次

在MVC中可以使用SQL查询吗?
用ASP.NET  MVC4做的一个项目,用的全是LINQ查询,我不想用这个查询,想用SQL查询,可以吗?怎么用啊?
sql mvc asp.net linq mvc4

------解决方案--------------------
可以的。

比如
ActionResult Index()
{
    List<Model> list = new List<Model>();
    string conStr = "...";
    SqlConnection conn = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand(sonStr, "select * from table");
    DataReader dr = cmd.ExecuteReader();
    while (dr.Read)
    {
        list.Add(new Model() { Name = dr.GetString(1), ID = dr.Int32(0), ... });
    }
    dr.Dispose();
    cmd.Dispose();
    conn.Close();
    return View(list);
}