如何通过 LINQ to SQL 更新和删除数据库中的记录?

发布于 2024-08-23 14:04:04 字数 29 浏览 9 评论 0原文

有能力做这个工作吗?我怎样才能完成这项工作?

Is there the ability for doing this work? How can I do this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-08-30 14:04:04

var context = new MyDataContext();
var newObj = new User();
newObj.UserID = 1;
newObj.Name = "泰德";

context.Users.InsertOnSubmit(newObj);  //queues for submission
context.SubmitChanges(); //submits to backend

或更新:

var context = new MyDataContext();
var user = context.Users.First(i => i.UserID = 1);
//entities self aware and automatically synced to database when a value changes
user.Name = "Dave";

context.SubmitChanges(); //knows about updated record

var context = new MyDataContext();
var newObj = new User();
newObj.UserID = 1;
newObj.Name = "Ted";

context.Users.InsertOnSubmit(newObj);  //queues for submission
context.SubmitChanges(); //submits to backend

or for update:

var context = new MyDataContext();
var user = context.Users.First(i => i.UserID = 1);
//entities self aware and automatically synced to database when a value changes
user.Name = "Dave";

context.SubmitChanges(); //knows about updated record
喜你已久 2024-08-30 14:04:04

当您使用 DataContext 生成 LINQ to SQL 查询时,它将跟踪从该上下文发出的查询中选择的对象。

话虽这么说,如果您对返回的对象进行更改,然后调用 DataContext 实例上的SubmitChanges 方法,更改将被持久化回底层数据存储。

如果要删除对象,则将该对象传递给 DeleteOnSubmit 方法Table 实例(其中 T 是数据库中表的模型类型)。然后,当您在 DataContext 上调用 SubmitChanges 时,传递给 DeleteOnSubmit 方法的模型表示的记录将被删除。

When you use a DataContext to generate a LINQ to SQL query, it will track objects that are selected from queries that eminate from that context.

That being said, if you make changes to the objects returned and then call the SubmitChanges method on the DataContext instance, the changes will be persisted back to the underlying data store.

If you want to delete an object, then you pass the object to the DeleteOnSubmit method on the Table<T> instance (where T is the type that is the model for the table in the database). Then, when you call SubmitChanges on the DataContext, the records represented by the models passed to the DeleteOnSubmit method will be deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文