2011-06-18 18 views
0

我正在设计一个asp.net mvc3项目,我在sql server中设计了数据库,并且使用ADO.net连接在App_Data中添加了这个项目。如何在asp.net中编写组合查询来访问数据mvc3

这是我ProductFormulation表

enter image description here

,这是我的原材料表

enter image description here

现在我想从原材料表得到代码这里的ProductID = 1 ProductFormulation 。 请告诉我应该写什么查询。我正在使用Razor视图引擎。

+1

你在linq.It写这与MVC或razor.Please无关请参阅http://msdn.microsoft.com/en-us/vcsharp/aa336746 var res =(来自RawMaterial中的x 在x.ID上的ProductFormulation中加入y等于y.ID 其中y.productID = 1 select y .Code) –

+0

我欣赏你的回答....先生请告诉我如何在我的看法(cshtml)中使用这个“res”。意味着我应该写什么编码来打印所有资源? –

回答

1
select rm.Code 
from RawMaterial rm 
    inner join ProductFormulation pf 
     on pf.RawMaterialId = rm.id 
where pf.productid = 1 

应该把你带到任何你需要去

+0

请写出在mvc3的控制器类中使用的查询。 –

+0

我不知道这是什么意思。如果你使用的是ado.net,你可以在mvc 3控制器中很好地使用该查询。如果您不使用ado.net,那么您使用的是什么。查询可以通过多种方式编写,完全有效。只有nhibernate有超过3种方法来编写查询,而那些只是你经常使用的方式。学习如何编程,但如果这太多,学习如何提出问题,以便您可以基于其他人的知识继续获利。 –

+0

是的,先生我正在使用ado.net。您的查询完全正确先生。现在我想知道如何在view.cshtml中打印这个结果。先生,我很抱歉。 –

0

写在乌尔控制器 - >任何功能

var res = (from x in RawMaterial 
join y in ProductFormulation on x.ID equals y.ID where y.productID = 1 select y.Code) 
return View(res);