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

一步一步学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_withparameter]

@customerid nchar(5),

@rowcount int output

as

set nocount on

set @rowcount = (select count(*) from customers where customerid = @customerid)

       使用同样的方法生成存储过程方法,然后使用下面的代码进行测试:

        int? rowcount = -1;

        ctx.sp_withparameter("", ref rowcount);

        Response.Write(rowcount);

        ctx.sp_withparameter("ALFKI", ref rowcount);

        Response.Write(rowcount);

       结果输出了“01”。说明ID为“”的顾客数为0,而ID为“ALFKI”的顾客数为1。存储过程的输出参数被封装成了ref参数,对于C#语法来说非常合情合理。SQL代码如下:

EXEC @RETURN_VALUE = [dbo].[sp_withparameter] @customerid = @p0, @rowcount = @p1 OUTPUT

-- @p0: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) []

-- @p1: InputOutput Int32 (Size = 0; Prec = 0; Scale = 0) [-1]

-- @RETURN_VALUE: Output Int32 (Size = 0; Prec = 0; Scale = 0) []

 

带返回值的存储过程

 

       再来创建第三个存储过程:

create proc [dbo].[sp_withreturnvalue]

@customerid nchar(5)

as

set nocount on

if exists (select 1 from customers where customerid = @customerid)

return 101

else

return 100

       生成方法后,可以通过下面的代码进行测试:

        Response.Write(ctx.sp_withreturnvalue(""));

        Response.Write(ctx.sp_withreturnvalue("ALFKI"));

       运行后程序输出“100101

0
0

数据库热门文章

    数据库最新文章

      最新新闻

        热门新闻