Linq to sql(二):DataContext与实体
时间:2010-08-31 来源:Jasmines
一. DataContext
DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库。
代码
var list = (from items in TestData.User_Table select items).OrderBy(item => item.UserID);
//var list = (from items in TestData.User_Table select new {用户ID=items.UserID,用户Name=items.UserName,用户 PWd=items.UserPwd });//------绑定DataGridView字段显示重写
this.listBox1.Items.Clear();
foreach (LinqTest.User_Table ut in list)
{
listBox1.Items.Add(ut.UserName);
}
this.dataGridView1.DataSource = list;
代码
LinqTest.User_Table ut = (from items in TestData.User_Table where items.UserName == this.listBox1.SelectedItem.ToString() select items).First();
TestData.User_Table.DeleteOnSubmit(ut);
TestData.SubmitChanges();
BindToData();
代码
LinqTest.User_Table ut = (from items in TestData.User_Table where items.UserName == this.listBox1.SelectedItem.ToString() select items).First();
this.txtID.Text = ut.UserID;
this.txtName.Text = ut.UserName;
this.txtPwd.Text = ut.UserPwd;
this.txtRoleID.Text = ut.RoleID.ToString();