文/ScottGu 译/韩现龙 出处/博客园
Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data.
上个月我开始了一个讲解LINQ to SQL的帖子系列。LINQ to SQL是集成在.NET Framework3.5中的O/RM(对象关系映射)的实现,它让你非常容易地用.NET类来生成关系型数据库的模型。然后你可以用LINQ 表达式对它来进行查询,更新,插入删除。Below are the first five parts of my LINQ to SQL series:
下边是我的该系列的前五篇的帖子的链接: - Part 1: Introduction to LINQ to SQL
- Part 2: Defining our Data Model Classes
- Part 3: Querying our Database
- Part 4: Updating our Database
- Part 5: Binding UI using the ASP:LinqDataSource Control
In these previous LINQ to SQL blog posts I demonstrated how you could use LINQ query expressions to programmatically retrieve data from a database.
在这前五篇帖子中,我说明了如何用LINQ的查询表达式通过编程的方式从数据库中检索数据。
In today's blog post I'll cover how you can also use database stored procedures (SPROCs) and user defined functions (UDFs) with your LINQ to SQL data model. Today's blog post will specifically cover how to call SPROCs to query and retrieve data from the database. In my next blog post in this series I'll then show how you can optionally also use SPROCs to update/insert/delete data from the database.
在今天的帖子中,我将会讲述一下如何和LINQ to SQL 数据模型一起来使用数据库中的存储过程(SPROCs)和自定义的函数(UDFs)。今天的博客会具体地讲一下如何调用存储过程从数据库中查询和检索数据库。在本系列的下五篇帖子中,我讲会讲一下如何选择用存储过程来更新/插入/删除数据。
To SPROC or not to SPROC? That is the question...
使用存储过程还是不使用?这是个问题....The question of whether to use Dynamic SQL generated by an ORM or instead use Stored Procedures when building a data layer is a topic that generates endless (very passionate) debate amongst developers, architects and DBAs. A lot of people much smarter than me have written on this topic, so I won't rehash the arguments for and against each side here. 在建立数据层时是使用ORM生成的动态SQL语句还是用存储过程这个问题在开发者,架构师和DBA之间一直争论不休。许多比我聪明的人已经写过此话题, 所以在此我不想重复地谈论是赞成还是反对这个问题。
The LINQ to SQL ORM that ships in .NET 3.5 is pretty flexible, and can be used to create data model classes whose object model can be independent of the underlying database schema, and which can
encapsulate business logic and validation rulesthat work regardless of whether the data model is populated/persisted via dynamic SQL or via SPROCs.
在.NET 3.5中的LINQ to SQL ORM非常灵活,你可以用来生成数据模型类,该数据模型类可以独立于数据库中的数据结构,而且可以写入无论是使用动态生成的SQL还是存储过程都可以执行的业务逻辑和验证规则。
In my
LINQ to SQL Part 3: Querying our Database post I discussed how you can write LINQ query expressions against a LINQ to SQL data model using code like below:
在第3部分
LINQ to SQL Part 3: Querying our Database:查询数据库这篇帖子中,我讨论了如何对LINQ to SQL数据模型像下面这样写LINQ的查询表达式:

附件:
您所在的用户组无法下载或查看附件When you write LINQ query expressions like this the LINQ to SQL ORM will execute the necessary dynamic SQL for you to retrieve Product objects that matches your query.
当你像这样写LINQ 查询表达式时,LINQ to SQL 模型将会执行必要的动态SQL语句来检索出跟你的查询匹配的产品对象。
As you'll learn in this post, you can also optionally map SPROCs in the database to your LINQ to SQL DataContext class, which allows you to alternatively retrieve the same Product objects by calling a stored procedure instead:
在这篇帖子中你将学到,你也可以选择将数据库中的存储过程遇到到你的LINQ to SQL DataContext类中,存储过程会允许你调用它来完成对产品对象的检索功能。

附件:
您所在的用户组无法下载或查看附件 This ability to use both dynamic SQL and SPROCs with a clean data model layer is pretty powerful, and provides a great deal of flexibility when working on projects.
这种数据模型的既能调用动态SQL又能调用存储过程能力是非常强大的,并且在对对象的操作上它提供了强大的灵活性。
The Steps to Map and Call a SPROC using LINQ to SQL
在LINQ to SQL中映射和调用存储过程的步骤
In my
Part 2: Defining our Data Model Classes tutorial I discussed how to use the LINQ to SQL ORM designer to create a LINQ to SQL class model like below:
在第二部分我讲述了如何用LINQ to SQL ORM设计器来生成一个如下的LINQ to SQL类模型:

附件:
您所在的用户组无法下载或查看附件Notice above how there are two panes on the LINQ to SQL ORM designer surface. The left pane enables us to define data model classes that map to our database. The right method pane allows us to optionally map SPROCs (and UDFs) to our LINQ to SQL DataContext object, which we can then use in-place of dynamic SQL to populate the data model objects.
注意,在上边的LINQ to SQL ORM设计器中有两个面板。左侧的面板使我们可以定义映射到我们数据库的数据模型。右侧的面板允许我们有选择的映射存储过程(和自定义的函数)到我们的LINQ to sQL DataContexxt对象,这个映射的存储过程可以允许我们用它来代替动态生成的SQL语句来从该数据模型对象中查找数据。
How to Map a SPROC to a LINQ to SQL DataContext
如何将存储过程映射到LINQ to SQL DataContextTo map SPROCs to our DataContext class, let's first go to the VS 2008 Server Explorer window and look at the SPROCs within our database:
为了映射存储过程到我们的DataContext类中,让我们产生到VS2008中的Server Explorer窗口,看一下在数据库中的存储过程:

附件:
您所在的用户组无法下载或查看附件We can double click any of the SPROCs above to open and edit them. For example, below is the "CustOrderHist" SPROC in Northwind:
我们可以双击存储过程来打开和编辑它们,例如,下面是在Northwind中的“CustOrderHist"的存储过程:

附件:
您所在的用户组无法下载或查看附件To map the above SPROC to our LINQ to SQL DataContext, we can drag/drop it from the Server Explorer onto our LINQ to SQL ORM designer. This will automatically create a new method on our LINQ to SQL DataContext class like below:
为了将上面的存储过程映射到我们的LINQ to SQL DataContext中,我们通过用拖放的方式将它从Server Explorer中拖到我们的LINQ to SQL ORM设计器中。这将自动地在LINQ to SQL DataContext类中生成如下的一个新方法:

附件:
您所在的用户组无法下载或查看附件By default the method name created on the DataContext class will be the same as the SPROC name, and the return type of the method will be an automatically created type that follows the "[SprocName]Result" naming pattern. For example: the SPROC above would return a sequence of "CustOrderHistResult" objects. We could optionally change the name of the method by selecting it in the designer and then use the property grid to rename it.
默认情况下,在DataContext为上生成的这个方法的方法名是和存储过程的名称是一样的,并且这个方法的返回值类型的命名方式是“[存储过程名称]结果”。例如,上面的存储过程将会返回一个"CustOrderHistResult"对象序列。我们可以选择将这个方法名进行更改:在设计器上选中它,用属性窗口来对它进行重命名。
How to Call our Newly Mapped SPROC
如何调用我们新映射的存储过程 Once we've done the steps above to map a SPROC onto our DataContext class, it is easy to use it to programmatically retrieve data. All we need to-do is call the new method we mapped on our DataContext class to get back a sequence of strongly typed results from the SPROC:
做完了上面的映射存储过程到我们的DataContext类的第一步之后,用它来进行编程的方式进行检索数据就很容易了。我们所做的只是来调用映射到我们的DataContext类上的方法来获取从存储过程中返回的一个强类型的序列。
Calling the SPROC in VB:在VB中调用存储过程: 
附件:
您所在的用户组无法下载或查看附件Calling the Sproc in C#:
在C#中调用存储过程:

附件:
您所在的用户组无法下载或查看附件In addition to programming looping over the result like in the code samples above, I could also obviously bind the results to any UI control to display them. For example, the below code databinds the result of our SPROC to a <asp:gridview> control:
除了像上面那样对结果集进行遍历之外,我还可以将结果集绑定到UI上并将它们显示出来。例如,下面的代码将我们的存储过程的结果集绑定到了<asp:gridview>控件上:

附件:
您所在的用户组无法下载或查看附件Which then displays the product history of our customer on a page like so:
在网页上它就会显示出我们客户的产品历史记录:

附件:
您所在的用户组无法下载或查看附件| 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。 |