您的位置:知识库 » 数据库

Linq To Sql进阶系列(五)Store Procedure篇

作者: Tom Song  来源: 博客园  发布时间: 2008-09-27 11:59  阅读: 9838 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

Linq To Sql进阶系列(一)从映射讲起

Linq To Sql进阶系列(二)M:M关系

Linq To Sql进阶系列(三)CUD和Log

Linq To Sql进阶系列(四)User Define Function篇

Linq To Sql进阶系列(五)Store Procedure篇

Linq To Sql进阶系列(六)用object的动态查询与保存log篇

Linq To Sql进阶系列(七)动态查询续及CLR与SQL在某些细节上的差别


选中该函数后,右击属性。就可以使用其他影射类。但是Linq会对返回的rowset做检查,如果发现返回结果和影射不匹配它会报错。而且一旦更改了,当你需要改回去的时候,你只能在Designer中删掉此sprocs,然后重新拖过来。
调用它很简单,就当作一个函数,但是,这里和普通的linq语句不一样的地方是,它不是延迟加载的。

            DataClasses1DataContext db = new DataClasses1DataContext();
            db.Log 
= Console.Out;
            var q 
= db.Customers_By_City("London");

正因它不是延迟加载的,所以,linq可以对他进行简单的内联操作,比如

            DataClasses1DataContext db = new DataClasses1DataContext();
            db.Log 
= Console.Out;

            var q 
= from c in db.Customers_By_City("London")
                    orderby c.City
                    select c;

注意的时,这里是Linq To Object而不是Linq To Sql。

2, MultipleResultSets
看下面的例子

CREATE PROCEDURE [dbo].[Get Customer And Orders](@CustomerID nchar(5))
    
-- Add the parameters for the stored procedure here
AS
BEGIN
    
-- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    
SELECT * FROM Customers AS c WHERE c.CustomerID = @CustomerID  
    
SELECT * FROM Orders AS o WHERE o.CustomerID = @CustomerID
END

使用OR designer对其影射,其dbml为

  <Function Name="dbo.[Get Customer And Orders]" Method="Get_Customer_And_Orders">
    
<Parameter Name="CustomerID" Parameter="customerID" Type="System.String" DbType="NChar(5)" />
    
<ElementType Name="Get_Customer_And_OrdersResult">
      
<Column Name="CustomerID" Type="System.String" DbType="NChar(5) NOT NULL"
CanBeNull
="false" />
      
<Column Name="CompanyName" Type="System.String" DbType="NVarChar(40) NOT NULL"
CanBeNull
="false" />
      
<Column Name="ContactName" Type="System.String" DbType="NVarChar(30)"
CanBeNull
="true" />
      
<Column Name="ContactTitle" Type="System.String" DbType="NVarChar(30)"
CanBeNull
="true" />
      
<Column Name="Address" Type="System.String" DbType="NVarChar(60)" CanBeNull="true" />
      
<Column Name="City" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="Region" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="PostalCode" Type="System.String" DbType="NVarChar(10)" CanBeNull="true" />
      
<Column Name="Country" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="Phone" Type="System.String" DbType="NVarChar(24)" CanBeNull="true" />
      
<Column Name="Fax" Type="System.String" DbType="NVarChar(24)" CanBeNull="true" />
    
</ElementType>
  
</Function>

用sqlmetal对它做影射,生成dbml为

<Function Name="dbo.Get Customer And Orders" Method="GetCustomerAndOrders">
    
<Parameter Name="CustomerID" Parameter="customerID" Type="System.String" DbType="NChar(5)" />
    
<ElementType Name="GetCustomerAndOrdersResult1">
      
<Column Name="CustomerID" Type="System.String" DbType="NChar(5)" CanBeNull="true" />
      
<Column Name="CompanyName" Type="System.String" DbType="NVarChar(40)"
CanBeNull
="true" />
      
<Column Name="ContactName" Type="System.String" DbType="NVarChar(30)"
CanBeNull
="true" />
      
<Column Name="ContactTitle" Type="System.String" DbType="NVarChar(30)"
CanBeNull
="true" />
      
<Column Name="Address" Type="System.String" DbType="NVarChar(60)" CanBeNull="true" />
      
<Column Name="City" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="Region" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="PostalCode" Type="System.String" DbType="NVarChar(10)"
 CanBeNull
="true" />
      
<Column Name="Country" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="Phone" Type="System.String" DbType="NVarChar(24)" CanBeNull="true" />
      
<Column Name="Fax" Type="System.String" DbType="NVarChar(24)" CanBeNull="true" />
    
</ElementType>
    
<ElementType Name="GetCustomerAndOrdersResult2">
      
<Column Name="OrderID" Type="System.Int32" DbType="Int" CanBeNull="true" />
      
<Column Name="CustomerID" Type="System.String" DbType="NChar(5)" CanBeNull="true" />
      
<Column Name="EmployeeID" Type="System.Int32" DbType="Int" CanBeNull="true" />
      
<Column Name="OrderDate" Type="System.DateTime" DbType="DateTime" CanBeNull="true" />
      
<Column Name="RequiredDate" Type="System.DateTime" DbType="DateTime"
CanBeNull
="true" />
      
<Column Name="ShippedDate" Type="System.DateTime" DbType="DateTime"
CanBeNull
="true" />
      
<Column Name="ShipVia" Type="System.Int32" DbType="Int" CanBeNull="true" />
      
<Column Name="Freight" Type="System.Decimal" DbType="Money" CanBeNull="true" />
      
<Column Name="ShipName" Type="System.String" DbType="NVarChar(40)" CanBeNull="true" />
      
<Column Name="ShipAddress" Type="System.String" DbType="NVarChar(60)"
CanBeNull
="true" />
      
<Column Name="ShipCity" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="ShipRegion" Type="System.String" DbType="NVarChar(15)" CanBeNull="true" />
      
<Column Name="ShipPostalCode" Type="System.String" DbType="NVarChar(10)"
CanBeNull
="true" />
      
<Column Name="ShipCountry" Type="System.String" DbType="NVarChar(15)"
CanBeNull
="true" />
    
</ElementType>
  
</Function>

0
0
 

数据库热门文章

    数据库最新文章

      最新新闻

        热门新闻