Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call LINQ in SQL Server database to execute stored procedure

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to call SQL Server database LINQ to execute stored procedures", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to call LINQ stored procedures in SQL Server database" this article.

How to call LINQ in SQL Server database to execute stored procedure. This article shows you how to call two different LINQ in the database to execute stored procedures. Each LINQ executes a stored procedure that returns the query result. One process takes input parameters, and the other takes no parameters.

The examples in this topic use the Northwind sample database. If you do not have the Northwind sample database installed on your development computer, you can download the database from the Microsoft Download Center (Microsoft download Center) website.

Description:

Your computer may display different names or locations for some of the Visual Studio user interface elements used in the following instructions. These elements depend on the version and settings of Visual Studio you are using. For more information, see Visual Studio Settings.

Create a connection to the database

1. In Visual Studio, open Server Explorer / Database Explorer by clicking Server Explorer / Database Explorer on the View menu.

two。 In Server Explorer / Database Explorer, right-click data connection, and then click add connection.

3. Specify a valid connection to the Northwind sample database.

Add a project that contains a LINQ to SQL file

1. In Visual Studio, on the File menu, point to New, and then click Project. Select Visual Basic Windows forms Application as the project type.

two。 On the Project menu, click add New. Select the LINQ to SQL Class item template.

3. Name the file northwind.dbml. Click add. Open the object Relational designer (Omap R designer) for the northwind.dbml file.

Add a stored procedure to the Omax R designer

1. In Server Explorer / Database Explorer, expand the connection to the Northwind database. Expand the stored procedures folder. If you have closed the Omax R designer, you can reopen it by double-clicking the previously added northwind.dbml file.

two。 Click the Sales by Year stored procedure and drag it to the right pane of the designer. Click the Ten Most Expensive Products stored procedure and drag it to the right pane of the designer.

3. Save your changes and close the designer.

4. Save your project.

Add code to display the results of a stored procedure

1. From the Toolbox, drag the DataGridView control onto the project's default Windows forms Form1.

two。 Double-click Form1 to add code to its Load event.

3. After you add the stored procedure to the Oamp R designer, the designer adds a DataContext object to the project. This object contains the code necessary to access these procedures. The project's DataContext object is named after the .dbml file name. For this project, the DataContext object is named northwindDataContext.

You can create an instance of DataContext in your code and call the stored procedure method specified by the Omax R designer. To bind to a DataGridView object, you must force the query to execute immediately by calling the ToList) method on the results of the stored procedure.

Add the following code to the Load event to call the stored procedure of any method exposed as a data context.

Visual Basic copies the code Dim db As New northwindDataContext 'Display the results of the Sales_by_Year stored procedure. DataGridView1.DataSource = _ db.Sales_by_Year (# 1 ToList ()...' Display the results of the Ten_Most_Expensive_Products' stored procedure. DataGridView1.DataSource = _ db.Ten_Most_Expensive_Products.ToList ()

4. Press F5 to run the project and view the results.

The above is all the contents of the article "how to call LINQ to execute stored procedures in SQL Server database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report