Linq to Sql之简单应用

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 ON
set QUOTED_IDENTIFIER ON
go

--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 output
as
if exists(select id from dbo.I_Table where id=@id)
begin
delete from I_Table where id=@id
set @Outp=1
end
else
set @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 output
as
insert 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 output
as
update dbo.I_Table set  I_Type=@I_Type where id=@id
set @OP=@@rowcount

--@@rowcount判断影响的行数


c#调用代码:

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



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值