您的位置:首页 > 数据库

Linq to SQL之使用存储过程 (1)

2007-07-10 15:28 585 查看
本文以Northwind数据库为例,说说Linq to SQL中是怎样使用存储过程。

首先我们创建一个存储过程:

create procedure dbo.linqDemo1
as
select * from customers

打开dbml设计器,在Server Explorer里面找个这个存储过程,把它拖拽到设计器里面,可以看到这个存储过程被映射为方法了

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }





这里有一点值得注意,(以存储过程linqDemo1为例)默认情况下,设计器对存储过程进行分析,将所有输出构造成一个结果类,运行该存储过程返回的就是这个类的对象集合。看看linqDemo1自动生成的代码:

[Function(Name="dbo.linqDemo1")]
public ISingleResult<linqDemo1Result> linqDemo1() {
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((ISingleResult<linqDemo1Result>)(result.ReturnValue));
}

LinqDemo1Result类就是生成的结果类,由于返回的customer表,该类的所有属性其实和customer的一模一样。 所以刚才拖拽存储过程的时候,如果把它拖拽到Customer类图的上面而不是周围的空白区域,生成的代码如下:

[Function(Name="dbo.linqDemo1")]
public ISingleResult<Customer> linqDemo1() {
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((ISingleResult<Customer>)(result.ReturnValue));
}

可以看到,它将存储过程返回的结果用Customer对象的集合来表示,而非创建一个自定义的结果类。

那么怎样运行存储过程呢?就和调用普通的成员方法一样,Linq to SQL将它映射到数据库中的存储过程:

NorthwindDataContext ctx = new NorthwindDataContext();
var results = ctx.linqDemo1();

是不是很简单?下面再来看看怎样调用带参数的存储过程

create procedure dbo.linqDemo2
@cID varchar(5)
as
select * from customers where customerID = @cID

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

然后利用设计器自动映射该存储过程,带参数的存储过程也就相应的映射为带参数的成员函数,怎样调用就不多说了。

[Function(Name="dbo.linqDemo2")]
public ISingleResult<Customer> linqDemo2([Parameter(DbType="VarChar(5)")] string cID) {
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), cID);
return ((ISingleResult<Customer>)(result.ReturnValue));
}

如果我们使用带output参数的存储过程呢?

create procedure dbo.linqDemo3
@cID varchar(5),
@msgCode int output
as
select * from customers where customerID = @cID
set @msgCode = 50

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

生成的成员函数是下面这样:

[Function(Name="dbo.linqDemo3")]
public ISingleResult<Customer> linqDemo3([Parameter(DbType="VarChar(5)")] string cID,
[Parameter(DbType="Int")] ref System.Nullable<int> msgCode) {
IExecuteResult result = this.ExecuteMethodCall(this,
((MethodInfo)(MethodInfo.GetCurrentMethod())), cID, msgCode);
msgCode = ((System.Nullable<int>)(result.GetParameterValue(1)));
return ((ISingleResult<Customer>)(result.ReturnValue));
}

可以看到它使用一个ref参数来保存返回参数的值,如下调用:

NorthwindDataContext ctx = new NorthwindDataContext();
int? output = 0;
var results = ctx.linqDemo3("ALFKI", ref output);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: