一步一步学Linq to sql(三):增删改
[2] 一步一步学Linq to sql(三):增删改
[3] 一步一步学Linq to sql(三):增删改
[4] 一步一步学Linq to sql(三):增删改
系列文章导航:
一步一步学Linq to sql(二):DataContext与实体
运行效果如下图:
在这里,我们通过Single方法获取一条记录,也就是一个tbGuestBook实例,更新了一些属性后保存也就完成了改这个操作。删除操作更简单,只需要从表中移除对象。你是不是觉得好像不是在操作数据库,像在操作内存中的对象。
由于写了日志,看看改和删操作会是怎么样的SQL?
UPDATE [dbo].[tbGuestBook] SET [IsReplied] = @p4, [Reply] = @p5 WHERE ([ID] = @p0) AND ([UserName] = @p1) AND ([PostTime] = @p2) AND ([Message] = @p3) AND (NOT ([IsReplied] = 1)) AND ([Reply] IS NULL) -- @p0: Input Guid (Size = 0; Prec = 0; Scale = 0) [00000000-0000-0000-0000-000000000000] -- @p1: Input String (Size = 4; Prec = 0; Scale = 0) [ghgh] -- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [2007-8-16 10:20:09] -- @p3: Input String (Size = 3; Prec = 0; Scale = 0) [ghj] -- @p4: Input Boolean (Size = 0; Prec = 0; Scale = 0) [True] -- @p5: Input String (Size = 3; Prec = 0; Scale = 0) [qqq] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
DELETE FROM [dbo].[tbGuestBook] WHERE ([ID] = @p0) AND ([UserName] = @p1) AND ([PostTime] = @p2) AND ([Message] = @p3) AND (NOT ([IsReplied] = 1)) AND ([Reply] = @p4) -- @p0: Input Guid (Size = 0; Prec = 0; Scale = 0) [158ec941-13ff-4093-bd8b-9fceae152171] -- @p1: Input String (Size = 2; Prec = 0; Scale = 0) [44] -- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [2007-8-16 9:56:19] -- @p3: Input String (Size = 2; Prec = 0; Scale = 0) [44] -- @p4: Input String (Size = 3; Prec = 0; Scale = 0) [222] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1 |
今天就讲到这里,下次将系统介绍查询句法。