[英]Fetch result from Raw SQL in asp.net MVC3
in controller 在控制器中
public ActionResult Index(int id)
{
string sql = string.Format("SELECT * FROM emp WHERE empno = {0}", 3195);
var ret = db.Database.SqlQuery<int>(sql).ToList();
// I want to fetch above query result here...
// Above query returns more than 1 rows and more than 1 column
// Like ret.Column1(row0).value
return RedirectToAction("Index");
}
how can I fetch result in the same function.. i need to perform some calculation here and saved the result in database again. 我如何在相同的函数中获取结果..我需要在这里执行一些计算并将结果再次保存在数据库中。
you need to do like this. 您需要这样做。 Create a ViewModel: 创建一个ViewModel:
public class EmployeeVM
{
public int Id {get;set;}
public string EmployeeName {get;set;}
}
action: 行动:
public ActionResult Index(int id)
{
string sql = string.Format("SELECT Id,EmployeeName FROM emp WHERE empno = {0}", 3195);
var ret = db.Database.SqlQuery<EmployeeVM>(sql).ToList();
/
return RedirectToAction("Index");
}
Remember that your model properties name should match the column name of the table, as in my case Id and EmployeeName property are mataching with column name. 请记住,模型属性名称应与表的列名称匹配,因为在我的案例中,Id和EmployeeName属性与列名称匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.