gpt4 book ai didi

.net - 无需先在 LINQ 2 SQL 中选择数据即可更新?

转载 作者:行者123 更新时间:2023-12-04 07:10:42 25 4
gpt4 key购买 nike

如何在不必先在 LINQ 中选择数据的情况下更新记录?

因为您必须首先执行 linq 选择(显然调用成本高昂的 SQL SELECT),更改所需的属性,然后执行 SubmitChanges()。我希望避免这种情况并只执行 SQL 更新,我已经知道 ID。

最佳答案

我还没有尝试过,但看起来你可以用 Attach 来做到这一点:

Table(TEntity).Attach Method (TEntity, Boolean)

您将对象设置为其更新状态并将其传入,将 bool 参数设置为 true。当然,那么您必须设置所有字段,而不仅仅是要更改的字段。

如果只想更改一两个字段,可以直接使用 ExecuteCommand 调用 SQL和 ExecuteQuery DataContext 对象上的方法。您必须将 SQL 作为字符串传入,并且 SQL 中的每个参数都有一个参数。请特别注意如何构造 SQL 字符串:

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.

关于.net - 无需先在 LINQ 2 SQL 中选择数据即可更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/399379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com