首先在数据库中随便创建两个存储过程,一个待参数,一个不待参数,
create proc sp_SelectStudent
as
begin
select * from Student
end
第二个:
create proc [dbo].[sp_AddClasses](@ClasseName nvarchar(20))
as
begin
insert into Classes values(@ClasseName)
end
然后在服务器资源管理器里面你可以看到如下图的两个存储过程:
我们把存储过程拖放到.DBML里,如下图:
我们在看看IDE为我们自动生成的代码,打开Demo.designer.cs
[Function(Name="dbo.sp_AddClasses")]
public int sp_AddClasses([Parameter(Name="ClasseName", DbType="NVarChar(20)")] string classeName)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), classeName);
return ((int)(result.ReturnValue));
}
这是Function函数IDE自动生成的,也许你在奇怪,IDE怎么知道我们的存储过程会返回什么类型的数据呢,其实在我们把存储过程拖入.DBML里的时候,IDE就执行了,EXEC sp_Addclasses 。
那么我们怎么试用存储过程呢,其他和调用函数一样:
DemoDataContext db = new DemoDataContext();
var result = from p in db.sp_SelectStudent() select p;
this.GridView1.DataSource = result;
this.GridView1.DataBind();
OK ,先写到这里,感觉充实多了!