Visual Studio 2008之新特性LINQ(二)

我们来编写一个selectTable()方法用来查询Myuser表,

ADO.NET:

 

     private   void  SelectTable()
    
{
        
this.comm = new SqlCommand();
        
this.comm.Connection = this.conn;
        
this.comm.CommandType = CommandType.Text;
        
this.comm.CommandText = "select * from MyUser";
        DataSet ds 
= new DataSet();
        
this.da = new SqlDataAdapter();
        
this.da.SelectCommand = this.comm;
        
this.da.Fill(ds);
        
this.dvShow.DataSource = ds.Tables[0].DefaultView;
        
this.dvShow.DataBind();
    }

LINQ:

 

     private   void  SelectTable()
    
{
        DataAccessDataContext ddc 
= new DataAccessDataContext();
        var select 
= from user in ddc.MyUsers
                     select user;

        
this.dvShow.DataSource = select;
        
this.dvShow.DataBind();
    }

大家可以看得很清楚了,使用LINQ极大地减少了代码编写的量,并且不再需要程序员编写SQL语句,处理起数据库操作就更加得心应手了。

        LINQ架构

       

LINQ的查询关键字:

-from

-where

-select

-group

-into

-orderby

-join

-let

与SQL语句的关键字多相似啊,相信大家通过一定时间的练习应该能很好的掌握。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值