更新没有在LINQ 2 SQL第一选择数据? [英] Update without first selecting data in LINQ 2 SQL?

查看:148
本文介绍了更新没有在LINQ 2 SQL第一选择数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能去更新记录,而无需在LINQ首先选择数据?

How can you go about updating a record without having to select the data first in LINQ?

正如你必须先执行选择一个LINQ(显然调用SQL SELECT是昂贵的),需要更改属性,然后执行的SubmitChanges()。我想避免这种情况,只是执行一个SQL UPDATE,我已经知道了ID。

As you must first perform a linq select (obviously calls a SQL SELECT which is costly), change required properties, then perform SubmitChanges(). I wish to avoid this and just perform a SQL UPDATE, I already know the ID.

推荐答案

我还没有尝试过,但看起来,如果你能做到这一点与附件:

I haven't tried it, but it looks as if you can do this with Attach:

表(TEntity)已.Attach方法(TEntity,布尔)

您设置对象在其更新的状态,并通过它,设定布尔参数为true。当然,你必须设置的所有的领域,不只是你想改变的。

You set up the object in its updated state and pass it in, setting the boolean param to true. Of course, then you have to set all the fields, not just the ones you want to change.

如果你只是想改变一个或两个字段,可以直接用的 的ExecuteCommand和的的executeQuery 上一个DataContext对象的方法。你必须传入SQL作为一个字符串,一个参数在SQL各个参数。请特别注意如何在SQL字符串,必须构建:

If you only want to change a field or two, you can call SQL directly with the ExecuteCommand and ExecuteQuery methods on a DataContext object. You have to pass in the SQL as a string, and one argument for each parameter in the SQL. Note in particular how the SQL string has to be constructed:

的语法命令几乎是   作为语法用于创建同一   一个ADO.NET DataCommand。唯一   区别在于如何参数   被指定。具体来说,您   通过引指定的参数   在大括号({...}),并一一列举   从1开始的参数   与同样编号的关联   对象中的参数数组。

The syntax for the command is almost the same as the syntax used to create an ADO.NET DataCommand. The only difference is in how the parameters are specified. Specifically, you specify parameters by enclosing them in braces ({…}) and enumerate them starting from 1. The parameter is associated with the equally numbered object in the parameters array.

这篇关于更新没有在LINQ 2 SQL第一选择数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆