So, here's my SQL query:
所以,这是我的SQL查询:
SELECT [Gender], count(*)
FROM [UsersDB].[dbo].[vwUser]
WHERE Gender='M'
GROUP BY Gender
In SMSS, this returns
在SMSS中,这将返回
M 5298
I want to print out that value (5298) in a div or a header on my webpage.
我想在我的网页上的div或标题中打印出该值(5298)。
So, in my Index.cshtml I tried
所以,在我的Index.cshtml中,我试过了
@using WebMatrix.Data;
@{
var db = Database.Open("usersdbconnectionstring");
var selectQueryString = "SELECT [Gender], count(*) FROM [UsersDB].[dbo].[vwUser] WHERE Gender='M' GROUP BY Gender";
}
I ripped that off from an example on W3C schools and it worked for printing out a table but not for this.
我从W3C学校的一个例子中扯掉了它,它可以用来打印一张桌子,但不是为了这个。
This didn't work either:
这也不起作用:
@WebMatrix.Data
@{
var db = Database.Open("usersdbconnectionstring");
var selectedData = db.Query("SELECT [Gender], count(*) FROM [UsersDB].[dbo].[vwUser] WHERE Gender='M' GROUP BY Gender");
var grid = new WebGrid(source: selectedData);
}
<div>
@grid.GetHtml()
</div>
Any ideas on how to get this to work?
有关如何使其工作的任何想法?
1 个解决方案
#1
0
In MVC
pattern, you should not implement SQL query in views. If you are working with Entity Framework
, you can do it like this:
在MVC模式中,您不应在视图中实现SQL查询。如果您正在使用Entity Framework,您可以这样做:
Model: add edmx
to project (See this).
型号:将edmx添加到项目中(见此)。
Controller: In your action
get data from db and pass to View
(See this).
控制器:在您的操作中从db获取数据并传递给View(请参阅此内容)。
View: Show data model in View (See this).
视图:在View中显示数据模型(参见此内容)。
#1
0
In MVC
pattern, you should not implement SQL query in views. If you are working with Entity Framework
, you can do it like this:
在MVC模式中,您不应在视图中实现SQL查询。如果您正在使用Entity Framework,您可以这样做:
Model: add edmx
to project (See this).
型号:将edmx添加到项目中(见此)。
Controller: In your action
get data from db and pass to View
(See this).
控制器:在您的操作中从db获取数据并传递给View(请参阅此内容)。
View: Show data model in View (See this).
视图:在View中显示数据模型(参见此内容)。