我们可以将Linq与sql server的View一起使用吗?

发布于 2024-10-17 16:46:41 字数 164 浏览 2 评论 0原文

根据我的理解,视图可以提高性能,我打算在数据库中使用视图,并且因为我的数据库很大,所以我想为不同的查询创建视图。有没有办法访问 .Net Windows 应用程序中的视图,或者我们可以将 LINQ 与视图一起使用吗?在.Net中使用View的确切方法是什么,以便我可以提高性能?我正在编写一个 C# 桌面应用程序。

As per my understanding Views could improve performance and I intend to use Views in my database and because my database is huge, I want to create the views for different queries. Is there any way to access the Views in .Net Windows applications or can we use LINQ with Views? What is the exact way to use View in .Net, so that i can improve performance? I am writing a C# desktop application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾夏年华 2024-10-24 16:46:41

“我想在 sql server 数据库中创建一个必须具有Where子句的视图。当我将其拖到.Net dbml文件中,并使用一些where时,必须询问参数。这可能吗?”

认为您是在询问在调用视图时是否可以将“where”参数传递给视图。这是不可能的,但您可以将参数传递给存储过程。此外,您的存储过程可以查询视图并使用参数对其进行过滤。下面是一个示例:

视图:名为“PersonView” - 为您提供所有内容(无Where子句)

SELECT     cit.CitizenID, cit.FirstName, cit.LastName, cit.OrganizationID, org.Name AS 'OrganizationName', 
FROM   Citizens AS cit JOIN Organizations AS org ON cit.OrganizationID = org.ID

存储过程:名为“spPersonQuery” - 执行您的Where子句

CREATE PROCEDURE [dbo].[spTest]
    -- Add the parameters for the stored procedure here
    @orgID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * from dbo.PersonView as ps
    where ps.OrganizationID = @orgID
END
GO

然后您可以将存储过程拖到DBML文件中,当您调用它时,您将传入 int 参数“orgID”。

"I want to create a view in sql server database that must have the Where clause. and when i will drag it to .Net dbml file, and use some where then must ask the parameter. can this possible."

I think you are asking if you can pass a "where" parameter to the View when it is called. This is not possible, but you can pass parameters to a Stored Procedure. Also, your stored procedure can query the View and use the param to filter it down. Here's an example:

VIEW: named "PersonView" -- gives you everything (no Where clause)

SELECT     cit.CitizenID, cit.FirstName, cit.LastName, cit.OrganizationID, org.Name AS 'OrganizationName', 
FROM   Citizens AS cit JOIN Organizations AS org ON cit.OrganizationID = org.ID

STORED PROCEDURE: named "spPersonQuery" -- does your Where clause

CREATE PROCEDURE [dbo].[spTest]
    -- Add the parameters for the stored procedure here
    @orgID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * from dbo.PersonView as ps
    where ps.OrganizationID = @orgID
END
GO

You can then drag your Stored Procedure into your DBML file and when you call it, you will pass in the int param "orgID".

我不咬妳我踢妳 2024-10-24 16:46:41

是的,只需将 Visual Studio 中的服务器资源管理器中的视图拖到 linq2sql 数据库模型上即可。

Yes, just drag the View from server explorer in visual studio onto your linq2sql database model.

ゝ杯具 2024-10-24 16:46:41

是的,您可以在 .NET 应用程序中使用视图。您可以像使用表格一样使用它。

例如:

var result = from v in TestView select v;

Yes, you can use a view in a .NET application. You use it in the same way you would use a table.

For example:

var result = from v in TestView select v;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文