Linq to Sql之简单应用

来源:互联网 发布:java断点续传框架 编辑:程序博客网 时间:2024/06/06 02:47

1,普通方式---增删改查

添加数据:

         Li = new Linq.Linq_I_TableDataContext();                //实例化一个表,并绑定数据                Linq.I_Table II = new Linq.I_Table();                II.I_Type = TypeText.Value;                II.I_Fund = FundText.Value;                Li.I_Table.InsertOnSubmit(II);                Li.SubmitChanges();                GetBind();


根据ID修改数据:

           Li = new Linq.Linq_I_TableDataContext();            Linq.I_Table II = Li.I_Table.Single(n => n.ID == Convert.ToInt32(Request["id"]));            II.I_Type = TypeText.Value;            II.I_Fund = FundText.Value;            Li.SubmitChanges();            GetBind();


删除数据:

          Li = new Linq.Linq_I_TableDataContext();            //lll.ObjectTrackingEnabled = false;            if (e.CommandName.Equals("del"))            {                int id = Convert.ToInt32(e.CommandArgument);                //从表中取出这一行数据                Linq.I_Table II = Li.I_Table.Single(n => n.ID == id);                Li.I_Table.DeleteOnSubmit(II);                Li.SubmitChanges();            }            GetBind();


遍历数据:

       public string OutData()        {            System.Text.StringBuilder sb = new System.Text.StringBuilder();            Li = new Linq.Linq_I_TableDataContext();            var a = from I_Table in Li.I_Table orderby I_Table.ID descending select I_Table;            var c = a.Skip(0).Take(5);                        foreach (var b in c)            {                sb.Append("<li>"+b.I_Type+"</li>");            }            return sb.ToString();        }


根据ID查找信息:

        private void GetId()        {            if (Request["id"] != null)            {                Li = new Linq.Linq_I_TableDataContext();                Linq.I_Table IT = Li.I_Table.Single(n => n.ID == Convert.ToInt32(Request["id"]));                TypeText.Value = IT.I_Type;                FundText.Value = IT.I_Fund;            }        }


2,LINQ调用存储过程

删除存储过程:

set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo--Return返回值--ALTER proc [dbo].[DeleteWithParam]--@id int--as--if exists(select id from dbo.I_Table where id=@id)--begin--delete from I_Table where id=@id--return 1--end--else--return 0--output返回值ALTER proc [dbo].[DeleteWithParam]@id int,@Outp int outputasif exists(select id from dbo.I_Table where id=@id)begindelete from I_Table where id=@idset @Outp=1endelseset @Outp=0


c#调用代码:

//Return返回值,1为正确,0为错误            Li = new Linq.Linq_I_TableDataContext();            // int aa = Li.DeleteWithParam(10);            //output返回值,直接判断output的值            int? aa = null;            Li.DeleteWithParam1(12, ref aa);            string bb = aa.ToString();


插入存储过程:

@@identity 为新插入的ID值,成功返回ID值,不成功为NULL

create proc  InsertReturn@I_Type nvarchar(50),@OP bigint outputasinsert into I_Table(I_Type) values(@I_Type)set @OP=@@identity


c#调用代码:

Li = new Linq.Linq_I_TableDataContext();            long? bb = null;            Li.InsertReturn("2", ref bb);            string aa = bb.ToString();


修改存储过程:

create proc  UpdateReturn@I_Type nvarchar(50),@id int,@OP bigint outputasupdate dbo.I_Table set  I_Type=@I_Type where id=@idset @OP=@@rowcount--@@rowcount判断影响的行数


c#调用代码:

Li = new Linq.Linq_I_TableDataContext();            long? bb = null;            Li.UpdateReturn("11", 13, ref bb);            string aa = bb.ToString();



 

原创粉丝点击