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

一步一步学Linq to sql(五):存储过程

作者: lovecherry  来源: 博客园  发布时间: 2008-09-26 23:04  阅读: 20632 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

一步一步学Linq to sql(一):预备知识

一步一步学Linq to sql(二):DataContext与实体

一步一步学Linq to sql(三):增删改

一步一步学Linq to sql(四):查询句法

一步一步学Linq to sql(五):存储过程

一步一步学Linq to sql(六):探究特性

一步一步学Linq to sql(七):并发与事务

一步一步学Linq to sql(八):继承与关系

一步一步学Linq to sql(九):其它补充

一步一步学Linq to sql(十):分层构架的例子


多结果集的存储过程

       再来创建一个多结果集的存储过程:

create proc [dbo].[sp_multiresultset]

as

set nocount on

select * from customers

select * from employees

       找到生成的存储过程方法:

[Function(Name="dbo.sp_multiresultset")]

    public ISingleResult<sp_multiresultsetResult> sp_multiresultset()

    {

        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));

        return ((ISingleResult<sp_multiresultsetResult>)(result.ReturnValue));

    }

       由于现在的VS2008会把多结果集存储过程识别为单结果集存储过程(只认识第一个结果集),我们只能对存储过程方法多小动手术,修改为:

    [Function(Name="dbo.sp_multiresultset")]

    [ResultType(typeof(Customer))]

    [ResultType(typeof(Employee))]

    public IMultipleResults sp_multiresultset()

    {

        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));

        return (IMultipleResults)(result.ReturnValue);

    }

       然后使用下面的代码测试:

        var 多结果集存储过程 = ctx.sp_multiresultset();

        var Customers = 多结果集存储过程.GetResult<Customer>();

        var Employees = 多结果集存储过程.GetResult<Employee>();

        GridView1.DataSource = from emp in Employees where emp.FirstName.Contains("A") select emp;

        GridView1.DataBind();

        GridView2.DataSource = from c in Customers where c.CustomerID.StartsWith("A") select c;

        GridView2.DataBind()


0
0

数据库热门文章

    数据库最新文章

      最新新闻

        热门新闻