DataSet ds = new DataSet();//创建数据集
string conStr = "Server=.;Database=ExpatiateAspNet;uid=sa;pwd="; //创建数据连接 using (SqlConnection con = new SqlConnection(conStr)) { //创建数据适配器 SqlDataAdapter sda = new SqlDataAdapter("select * from UserInfo", con); con.Open(); //打开数据连接 sda.Fill(ds, "UserInfo");//数据适配器从数据源中取得数据填充到数据集中 } //使用LINQ返回数据集表中用户名等于"云峰"或"Admin"的序列 var query = from item in ds.Tables["UserInfo"].AsEnumerable() where item.Field<string>("UserName") == "云峰" || item.Field<string>("UserCode") == "Admin" select item;DataView dv = query.AsDataView();//将序列转换为DataView
DataTable dt = query.CopyToDataTable();//将序列转换为DataTablegvDataView.DataSource = dv;//将DataView绑定到GridView
gvDataView.DataBind();gvDataTable.DataSource = dt;//将DataTable绑定到GridView
gvDataTable.DataBind();