How to: Call a Stored Procedure by Using LINQ (Visual Basic)
Language-Integrated Query (LINQ) makes it easy to access database information, including database objects such as stored procedures.
The following example shows how to create an application that calls a stored procedure in a SQL Server database. The sample shows how to call two different stored procedures in the database. Each procedure returns the results of a query. One procedure takes input parameters, and the other procedure does not take parameters.
The examples in this topic use the Northwind sample database. If you do not have the Northwind sample database on your development computer, you can download it from the Microsoft Download Center Web site. For instructions, see Downloading Sample Databases (LINQ to SQL).
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
To create a connection to a database
In Visual Studio, open Server Explorer/Database Explorer by clicking Server Explorer/Database Explorer on the View menu.
Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.
Specify a valid connection to the Northwind sample database.
To add a project that contains a LINQ to SQL file
In Visual Studio, on the File menu, point to New and then click Project. Select Visual Basic Windows Forms Application as the project type.
On the Project menu, click Add New Item. Select the LINQ to SQL Classes item template.
Name the file northwind.dbml. Click Add. The Object Relational Designer (O/R Designer) is opened for the northwind.dbml file.
To add stored procedures to the O/R Designer
In Server Explorer/Database Explorer, expand the connection to the Northwind database. Expand the Stored Procedures folder.
If you have closed the O/R Designer, you can reopen it by double-clicking the northwind.dbml file that you added earlier.
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 drag it to the right pane of the designer.
Save your changes and close the designer.
Save your project.
To add code to display the results of the stored procedures
From the Toolbox, drag a DataGridView control onto the default Windows Form for your project, Form1.
Double-click Form1 to add code to its Load event.
When you added stored procedures to the O/R Designer, the designer added a DataContext object for your project. This object contains the code that you must have to access those procedures. The DataContext object for the project is named based on the name of the .dbml file. For this project, the DataContext object is named northwindDataContext.
You can create an instance of the DataContext in your code and call the stored procedure methods specified by the O/R Designer. To bind to the DataGridView object, you may have to force the query to execute immediately by calling the ToList<TSource> method on the results of the stored procedure.
Add the following code to the Load event to call either of the stored procedures exposed as methods for your data context.
Dim db As New northwindDataContext ' Display the results of the Sales_by_Year stored procedure. DataGridView1.DataSource = _ db.Sales_by_Year(#1/1/1996#, #1/1/2007#).ToList() ... ' Display the results of the Ten_Most_Expensive_Products ' stored procedure. DataGridView1.DataSource = _ db.Ten_Most_Expensive_Products.ToList()
Press F5 to run your project and view the results.