Walkthrough: Using Only Stored Procedures (Visual Basic) (LINQ to SQL)
This walkthrough provides a basic end-to-end LINQ to SQL scenario for accessing data by using stored procedures only. This approach is often used by database administrators to limit how the datastore is accessed.
You can also use stored procedures in LINQ to SQL applications to override default behavior, especially for Create, Update, and Delete processes. For more information, see Customizing Insert, Update, and Delete Operations (LINQ to SQL).
For purposes of this walkthrough, you will use two methods that have been mapped to stored procedures in the Northwind sample database: CustOrdersDetail and CustOrderHist. The mapping occurs when you run the SqlMetal command-line tool to generate a Visual Basic file. For more information, see the Prerequisites section later in this walkthrough.
This walkthrough does not rely on the Object Relational Designer. Developers using Visual Studio can also use the O/R Designer to implement stored procedure functionality. Object Relational Designer (O/R Designer)
Object Relational Designer (O/R Designer)
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.
This walkthrough was written by using Visual Basic Development Settings.
This walkthrough requires the following:
This walkthrough uses a dedicated folder ("c:\linqtest3") to hold files. Create this folder before you begin the walkthrough.
The Northwind sample database.
If you do not have this database on your development computer, you can download it from the Microsoft download site. For instructions, see Downloading Sample Databases (LINQ to SQL). After you have downloaded the database, copy the northwnd.mdf file to the c:\linqtest3 folder.
A Visual Basic code file generated from the Northwind database.
This walkthrough was written by using the SqlMetal tool with the following command line:
sqlmetal /code:"c:\linqtest3\northwind.vb" /language:vb "c:\linqtest3\northwnd.mdf" /sprocs /functions /pluralize
For more information, see Code Generation Tool (SqlMetal.exe).
In this first task, you create a Visual Studio solution that contains the necessary references to build and run a LINQ to SQL project.
To create a LINQ to SQL solution
On the Visual Studio File menu, click New Project.
In the Project types pane in the New Project dialog box, expand Visual Basic, and then click Windows.
In the Templates pane, click Windows Forms Application.
In the Name box, type SprocOnlyApp.
The Windows Forms Designer opens.
The LINQ to SQL assembly is not included in the standard Windows Forms Application template. You will have to add the assembly yourself, as explained in the following steps:
To add System.Data.Linq.dll
In Solution Explorer, click Show All Files.
In Solution Explorer, right-click References, and then click Add Reference.
In the Add Reference dialog box, click .NET, click the System.Data.Linq assembly, and then click OK.
The assembly is added to the project.
This step assumes that you have used the SqlMetal tool to generate a code file from the Northwind sample database. For more information, see the Prerequisites section earlier in this walkthrough.
To add the northwind code file to the project
On the Project menu, click Add Existing Item.
In the Add Existing Item dialog box, move to c:\linqtest3\northwind.vb, and then click Add.
The northwind.vb file is added to the project.
In this step, you define the connection to the Northwind sample database. This walkthrough uses "c:\linqtest3\northwnd.mdf" as the path.
To create the database connection
In this task you create an interface so that users can execute stored procedures to access data in the database. In the application that you are developing with this walkthrough, users can access data in the database only by using the stored procedures embedded in the application.
To set up the user interface
Return to the Windows Forms Designer (Form1.vb[Design]).
On the View menu, click Toolbox.
The toolbox opens.
Click the AutoHide pushpin to keep the toolbox open while you perform the remaining steps in this section.
Drag two buttons, two text boxes, and two labels from the toolbox onto Form1.
Arrange the controls as in the accompanying illustration. Expand Form1 so that the controls fit easily.
Right-click Label1, and then click Properties.
Change the Text property from Label1 to Enter OrderID:.
In the same way for Label2, change the Text property from Label2 to Enter CustomerID:.
In the same way, change the Text property for Button1 to Order Details.
Change the Text property for Button2 to Order History.
Widen the button controls so that all the text is visible.
To handle button clicks
Double-click Order Details on Form1 to create the Button1 event handler and open the code editor.
Type the following code into the Button1 handler:
' Declare a variable to hold the contents of ' TextBox1 as an argument for the stored ' procedure. Dim parm As String = TextBox1.Text ' Declare a variable to hold the results returned ' by the stored procedure. Dim custQuery = db.CustOrdersDetail(parm) ' Clear the message box of previous results. Dim msg As String = "" Dim response As MsgBoxResult ' Execute the stored procedure and store the results. For Each custOrdersDetail As CustOrdersDetailResult In custQuery msg &= custOrdersDetail.ProductName & vbCrLf Next ' Display the results. If msg = "" Then msg = "No results." End If response = MsgBox(msg) ' Clear the variables before continuing. parm = "" TextBox1.Text = ""
Now double-click Button2 on Form1 to create the Button2 event handler and open the code editor.
Type the following code into the Button2 handler:
' Comments in the code for Button2 are the same ' as for Button1. Dim parm As String = TextBox2.Text Dim custQuery2 = db.CustOrderHist(parm) Dim msg As String = "" Dim response As MsgBoxResult For Each custOrdHist As CustOrderHistResult In custQuery2 msg &= custOrdHist.ProductName & vbCrLf Next If msg = "" Then msg = "No results." End If response = MsgBox(msg) parm = "" TextBox2.Text = ""
Now it is time to test your application. Note that your contact with the datastore is limited to whatever actions the two stored procedures can take. Those actions are to return the products included for any orderID you enter, or to return a history of products ordered for any CustomerID you enter.
To test the application
Press F5 to start debugging.
In the Enter OrderID box, type 10249 and then click Order Details.
A message box lists the products included in order 10249.
Click OK to close the message box.
In the Enter CustomerID box, type ALFKI, and then click Order History.
A message box lists the order history for customer ALFKI.
Click OK to close the message box.
In the Enter OrderID box, type 123, and then click Order Details.
A message box displays "No results."
Click OK to close the message box.
On the Debug menu, click Stop debugging.
The debug session closes.
If you have finished experimenting, you can click Close Project on the File menu, and save your project when you are prompted.