我们来编写一个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();
}
... {
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();
}
... {
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语句的关键字多相似啊,相信大家通过一定时间的练习应该能很好的掌握。