首页 / C# / Linq to sql-存储过程
Linq to sql-存储过程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linq to sql-存储过程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2566字,纯文字阅读大概需要4分钟。
内容图文
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”
多结果集的存储过程
再来创建一个多结果集的存储过程:
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();
使用存储过程新增数据
Linq to sql-存储过程
标签:
本文系统来源:http://www.cnblogs.com/dekevin/p/5851829.html
内容总结
以上是互联网集市为您收集整理的Linq to sql-存储过程全部内容,希望文章能够帮你解决Linq to sql-存储过程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。