用ADO.NET與資料庫連線,產生DBcontext,在controoller撈資料的語法
//LinQ
public ActionResult Details(int? CM_PKey)
{
List<CompanyMain> query = (from c in context.CompanyMains
where c.CM_PKey == CM_PKey
select c).Take(1).ToList();
return View(query[0]);
}
//SqlQuery
public ActionResult Details(int? CM_PKey)
{
List<CompanyMain> liCompanyMain = context.CompanyMains.SqlQuery(
@"select * from CompanyMain Where CM_PKey IN ({0})", CM_PKey.ToString()).ToList();
return View(liCompanyMain[0]);
}
//FirstOrDefault
public ActionResult Details(string sPermID)
{
CompanyMain oCompanyMain = context.CompanyMains.FirstOrDefault<CompanyMain>(o => o.CM_PermID == sPermID);
return View(oCompanyMain);
}
//Find
public ActionResult Details(int? CM_PKey)
{
CompanyMain oCompanyMain = context.CompanyMains.Find(CM_PKey);
if (oCompanyMain == null)
return HttpNotFound();
return View(oCompanyMain);
}
//Join View
public ActionResult DetailsCompanyOpera(int id)
{
List<ViewCompanyMainOpera> liViewCompanyMainOpera = context.ViewCompanyMainOperas.SqlQuery(
@"select * from ViewCompanyMainOpera Where CM_PKey IN ({0})", id.ToString()).ToList();
return View(liViewCompanyMainOpera[0]);
}
0 意見:
張貼留言