您的位置:首页 > 其它

Visual Studio 2008之新特性LINQ(二)

2008-05-19 10:42 330 查看
我们来编写一个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语句的关键字多相似啊,相信大家通过一定时间的练习应该能很好的掌握。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: