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


    普通存储过程

    create proc sp_singleresultset
    as
    set nocount on
    select * from customers  
    

    首先在查询分析器运行下面的代码来创建一个存储过程sp_singleresultset。然后打开IDE的服务器资源管理器,我们从存储过程中找到刚才创建的存储过程,然后拖动到设计视图。在方法面板中可以看到已经创建了一个sp_singleresultset的方法,如下图:

    打开GuestBook.designer.cs,可以找到如下代码

    		[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.sp_singleresultset")]
    		public ISingleResult<sp_singleresultsetResult> sp_singleresultset()
    		{
    			IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
    			return ((ISingleResult<sp_singleresultsetResult>)(result.ReturnValue));
    		}
    

      然后调用

                GuestBookDataContext Book=new GuestBookDataContext();
                var GetResult = (from c in Book.sp_singleresultset()
                                where c.CustomerID.StartsWith("A")
                                select c).ToList();

    结果如下

    带参数和返回值的存储过程

    首先还是在查询分析器中执行如下SQL

    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

    还是在IDE服务资源管理器中将存储过程拖进dbml文件中,然后在代码中进行调用

                string str = Book.sp_withreturnvalue("").ToString();
                str = str + Book.sp_withreturnvalue("ALFKI");

    结果如下

    使用存储过程新增数据 

    在查询分析器中执行如下SQL

    create proc sendmessage
    @username varchar(50),
    @message varchar(500)
    as
    insert into tbGuestBook
    (id,username,posttime,[message],IsRequired,reply)
    values
    (newid(),@username,getdate(),@message,0,'')

    然后,打开留言簿dbml,把存储过程从服务器资源管理器拖拽到设计视图上。右键点击tbGuestBook实体类,选择配置行为。如下图,为插入操作选择刚才创建的存储过程方法,并进行参数匹配:

    这是添加之前, 可以参看 Linq To SQL(三):增删查改 http://www.cnblogs.com/aehyok/archive/2013/04/12/3017545.html

            public ActionResult SaveBook(tbGuestBook tb)
            {
                tbGuestBook gb = null;
                if (tb.ID.ToString() != "00000000-0000-0000-0000-000000000000")
                {
                    gb = ctx.tbGuestBook.Single(b => b.ID == new Guid(tb.ID.ToString()));
                    gb.PostTime = DateTime.Now;
                    gb.UserName = tb.UserName;
                    gb.Message = tb.Message;
                    ctx.SubmitChanges();
                    return Index();
                }
                else
                {
                    tb.ID = Guid.NewGuid();
                    tb.IsRequired = false;
                    tb.PostTime = DateTime.Now;
                    ctx.tbGuestBook.InsertOnSubmit(tb);
                    ctx.SubmitChanges();
                }
                    return Index();
            }
    

      看else下面tb实体类,还需要给ID,PostTime,IsRequired赋值,这样才能添加操作。

    现在可以修改代码,将else代码改为

                    ctx.tbGuestBook.InsertOnSubmit(tb);
                    ctx.SubmitChanges();
    

      

    tb实体只需要两个参数即可添加到数据库中。

  • 相关阅读:
    SQL查询语句大全集锦
    SQL Union和SQL Union All用法
    SQL Server中替换函数STUFF、replace的使用
    oscache的使用(例子)
    SQL CURSOR 游标 存储过程
    折腾iPhone的生活——AirDrop的使用
    折腾iPhone的生活——通过设置使iPhone更省电
    折腾iPhone的生活——iPhone 5s 开启 assistive touch 后卡顿的问题
    vimium快捷键列表
    在MacOSX下使用Github管理Xcode代码
  • 原文地址:https://www.cnblogs.com/aehyok/p/3027109.html
Copyright © 2020-2023  润新知