Académique Documents
Professionnel Documents
Culture Documents
http://msdn.microsoft.com/en-us/library/bb399407
Note
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 C# 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.
Note
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 C# Development Settings.
Prerequisites
This walkthrough requires the following: This walkthrough uses a dedicated folder ("c:\linqtest7") 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:\linqtest7 folder. A C# code file generated from the Northwind database. This walkthrough was written by using the SqlMetal tool with the following command line: sqlmetal /code:"c:\linqtest7\northwind.cs" /language:csharp "c:\linqtest7 \northwnd.mdf" /sprocs /functions /pluralize For more information, see SqlMetal.exe (Code Generation Tool).
Overview
1 of 6 5/25/2012 11:39 AM
Walkthrough: Using Only Stored Procedures (C#) (LINQ to SQL) This walkthrough consists of six main tasks: Setting up the LINQ to SQL solution in Visual Studio. Adding the System.Data.Linq assembly to the project. Adding the database code file to the project. Creating a connection with the database. Setting up the user interface. Running and testing the application.
http://msdn.microsoft.com/en-us/library/bb399407
To add System.Data.Linq.dll
1. In Solution Explorer, right-click References, and then click Add Reference. 2. 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.
2 of 6
5/25/2012 11:39 AM
Walkthrough: Using Only Stored Procedures (C#) (LINQ to SQL) The northwind.cs file is added to the project.
http://msdn.microsoft.com/en-us/library/bb399407
Note
Click the AutoHide pushpin to keep the toolbox open while you perform the remaining steps in this section. 3. 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. 4. Right-click label1, and then click Properties. 5. Change the Text property from label1 to Enter OrderID:. 6. In the same way for label2, change the Text property from label2 to Enter CustomerID:. 7. In the same way, change the Text property for button1 to Order Details. 8. Change the Text property for button2 to Order History. Widen the button controls so that all the text is visible.
3 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb399407
// Declare a variable to hold the contents of // textBox1 as an argument for the stored // procedure. string param = textBox1.Text; // Declare a variable to hold the results // returned by the stored procedure. var custquery = db.CustOrdersDetail(Convert.ToInt32(param)); // Execute the stored procedure and display the results. string msg = ""; foreach (CustOrdersDetailResult custOrdersDetail in custquery) { msg = msg + custOrdersDetail.ProductName + "\n"; } if (msg == "") msg = "No results."; MessageBox.Show(msg); // Clear the variables before continuing. param = ""; textBox1.Text = "";
3. Now double-click button2 on Form1 to open the button2 handler 4. Type the following code into the button2 handler:
// Comments in the code for button2 are the same // as for button1. string param = textBox2.Text;
4 of 6
5/25/2012 11:39 AM
Walkthrough: Using Only Stored Procedures (C#) (LINQ to SQL) var custquery = db.CustOrderHist(param); string msg = ""; foreach (CustOrderHistResult custOrdHist in custquery) { msg = msg + custOrdHist.ProductName + "\n"; } MessageBox.Show(msg); param = ""; textBox2.Text = "";
http://msdn.microsoft.com/en-us/library/bb399407
Next Steps
You can enhance this project by making some changes. For example, you could list available stored procedures in a list box and have the user select which procedures to execute. You could also stream the output of the reports to a text file.
See Also
Concepts
5 of 6
5/25/2012 11:39 AM
Walkthrough: Using Only Stored Procedures (C#) (LINQ to SQL) Learning by Walkthroughs (LINQ to SQL) Other Resources Stored Procedures (LINQ to SQL)
http://msdn.microsoft.com/en-us/library/bb399407
Yes
No
6 of 6
5/25/2012 11:39 AM