Code
1
2
3 //在GuestBook.designer.cs里有如下代码
4
5 //GuestBookDataContext这个类继承了System.Data.Linq.DataContext
6 //DataContext去做链接数据库
7 [System.Data.Linq.Mapping.DatabaseAttribute(Name="GuestBook")]
8 public partial class GuestBookDataContext : System.Data.Linq.DataContext
9 public GuestBookDataContext() :
10
11 //构造方法里会去做数据库连接
12
13
14 //tbGuestBook类 做表映射
15 [Table(Name="dbo.tbGuestBook")]
16 public partial class tbGuestBook
17
18 ---------添加方法
19 //实例一个数据库连接
20 GuestBookDataContext cont = new GuestBookDataContext();
21 //实例一个对象
22 tbGuestBook tgb = new tbGuestBook();
23 //设置属性
24 tgb.ID = Guid.NewGuid();
25 tgb.UserName = tb_UserName.Text;
26 tgb.Message = tb_Message.Text;
27 tgb.IsReplied = false;
28 tgb.PostTime = DateTime.Now;
29 //添加方法
30 cont.tbGuestBook.InsertOnSubmit(tgb);
31 //提交
32 cont.SubmitChanges();
33
34 --------删除方法
35
36 GuestBookDataContext cont = new GuestBookDataContext();
37 //tbGuestBook对象不是new出来的,是通过数据库查询出来的
38 tbGuestBook tb = cont.tbGuestBook.Single(b => b.ID == new Guid("ffd21"));
39
40 cont.tbGuestBook.DeleteOnSubmit(tb);
41
42 cont.SubmitChanges();
43
44 ------修改
45 //tbGuestBook对象不是new出来的,是通过数据库查询出来的
46 tbGuestBook tb = cont.tbGuestBook.Single(b => b.ID == new Guid("33232"));
47
48 tb.Reply = "hhh";
49
50 tb.IsReplied = true;
51
52 cont.SubmitChanges();
53
1
2
3 //在GuestBook.designer.cs里有如下代码
4
5 //GuestBookDataContext这个类继承了System.Data.Linq.DataContext
6 //DataContext去做链接数据库
7 [System.Data.Linq.Mapping.DatabaseAttribute(Name="GuestBook")]
8 public partial class GuestBookDataContext : System.Data.Linq.DataContext
9 public GuestBookDataContext() :
10
11 //构造方法里会去做数据库连接
12
13
14 //tbGuestBook类 做表映射
15 [Table(Name="dbo.tbGuestBook")]
16 public partial class tbGuestBook
17
18 ---------添加方法
19 //实例一个数据库连接
20 GuestBookDataContext cont = new GuestBookDataContext();
21 //实例一个对象
22 tbGuestBook tgb = new tbGuestBook();
23 //设置属性
24 tgb.ID = Guid.NewGuid();
25 tgb.UserName = tb_UserName.Text;
26 tgb.Message = tb_Message.Text;
27 tgb.IsReplied = false;
28 tgb.PostTime = DateTime.Now;
29 //添加方法
30 cont.tbGuestBook.InsertOnSubmit(tgb);
31 //提交
32 cont.SubmitChanges();
33
34 --------删除方法
35
36 GuestBookDataContext cont = new GuestBookDataContext();
37 //tbGuestBook对象不是new出来的,是通过数据库查询出来的
38 tbGuestBook tb = cont.tbGuestBook.Single(b => b.ID == new Guid("ffd21"));
39
40 cont.tbGuestBook.DeleteOnSubmit(tb);
41
42 cont.SubmitChanges();
43
44 ------修改
45 //tbGuestBook对象不是new出来的,是通过数据库查询出来的
46 tbGuestBook tb = cont.tbGuestBook.Single(b => b.ID == new Guid("33232"));
47
48 tb.Reply = "hhh";
49
50 tb.IsReplied = true;
51
52 cont.SubmitChanges();
53