In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 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 solve the Linq stored procedure return problem", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to solve the Linq stored procedure return problem" this article.
Stored procedure
When we write programs, we often need some stored procedures. How to use them in LINQ to SQL? Maybe it's simpler than the original one. Let's take a look at a few stored procedures that come with the NORTHWND.MDF database.
1. Scalar return
In the database, there is a stored procedure called Customers Count By Region. The Linq stored procedure returns the number of customers in the "WA" area.
ALTER PROCEDURE [dbo]. [NonRowset] (@ param1 NVARCHAR (15)) AS BEGIN SET NOCOUNT ON; DECLARE @ count int SELECT @ count = COUNT (*) FROM Customers WHERECustomers.Region = @ Param1 RETURN @ count END
All we have to do is drag the stored procedure into the Ohand R designer, which automatically generates the following code snippet:
[Function (Name = "dbo. [Customers Count By Region]")] public int Customers_Count_By_Region ([Parameter (DbType = "NVarChar (15)")] string param1) {IExecuteResult result = this.ExecuteMethodCall (this, ((MethodInfo) (MethodInfo.GetCurrentMethod ()), param1); return ((int) (result.ReturnValue));}
When we need it, we can just call it directly, for example:
Int count = db.CustomersCountByRegion ("WA"); Console.WriteLine (count)
Statement description: this example uses the Linq stored procedure to return the number of customers in the "WA" area.
two。 Single result set
Returns a rowset from the database and contains input parameters for filtering results. When we execute a stored procedure that returns a rowset, we use the result class, which stores the results returned from the stored procedure.
The following example represents a stored procedure that returns customer rows and uses input parameters to return only fixed columns of those rows that list "London" as the customer city.
ALTER PROCEDURE [dbo]. [Customers By City]-Add the parameters for the stored procedure here (@ param1 NVARCHAR (20)) AS BEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements. SET NOCOUNT ON; SELECT CustomerID, ContactName, CompanyName, City from Customers as c where c.City=@param1 END
Drag it into the Ocord R designer, which automatically generates the following code snippet:
[Function (Name= "dbo. [Customers By City]")] public ISingleResult Customers_By_City ([Parameter (DbType= "NVarChar (20)")] string param1) {IExecuteResult result = this.ExecuteMethodCall (this, ((MethodInfo) (MethodInfo.GetCurrentMethod ()), param1); return ((ISingleResult) (result.ReturnValue));}
We call it with the following code:
ISingleResult result = db.Customers_By_City ("London"); foreach (Customers_By_CityResult cust in result) {Console.WriteLine ("CustID= {0}; City= {1}", cust.CustomerID, cust.City);}
Statement description: this example uses the Linq stored procedure to return the CustomerID and City of the customer in London.
These are all the contents of the article "how to solve the problem of returning Linq stored procedures". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.