Winforms 中使用 linq-to-sql 进行 CRUD

发布于 2024-09-12 09:53:47 字数 393 浏览 11 评论 0原文

我有一个简单的 winforms 应用程序,我使用 linq-to-sql 连接到我的数据库。

我直接从数据库生成了类,并且有一个 DataAccess 类来包装我的数据上下文,并可以为我提供所需的任何内容。

我有一个视图,它使用对象数据源来填充 DataGridView 和一组相关文本字段等。对于我的实体(我们称之为 EmployeeView),

该视图加载所有现有行,当我单击网格时,字段会适当更新。

如果我更改字段,更改将通过记录更改持续存在,但我不确定如何通过数据访问层保存更改。如何检测哪些记录是脏的并且需要保存?那我该如何拯救他们呢?添加新记录的最佳方式是什么?删除记录?

我可以在网上找到很多资源,但没有一个包含我需要的示例。谁能帮我解决一些基本模式,或者给我指出一个好地方?

I have a simple winforms application that I am connecting to my database with linq-to-sql.

I have generated the classes directly from the database, and I have a DataAccess class that wraps my datacontext and can give me whatever I need.

I have a view that uses an object datasource to populate a DataGridView and a set of related text fields, etc.. for my entity (lets call it EmployeeView)

The view loads all of the existing rows, and as I click through the grid, the fields update appropriately.

If I change fields, the changes will persist through record changes, but I am not sure how to save changes through my data access layer. How can I detect which records are dirty and need to be saved? How do I then save them? What is the best way to add new records? Delete records?

I can find a lot of resources online, but none with the kind of examples I need. Can anyone help me out with some of the basic patterns, or point me to a good place?

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

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

发布评论

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

评论(1

无声情话 2024-09-19 09:53:47

我认为,使用 LINQ-to-SQL 类的最基本方法是实例化它们的列表(例如,让我们使用 Employee)来包含您希望(可能)编辑的Employees。当这些 Employee 对象的属性发生更改时,这些对象会自动“变脏”,并且调用 DataContext.SubmitChanges() 将保留它们。

List<Employee> employees = (from e in dataContext.Employees where e.Salary > 50000 select e).toList();

foreach(var employee in employees)
{
  employee.CanAffordToyotaPrius = true;
}

dataContext.SubmitChanges();

如果您要包装 DataContext 并且仅使用 DataGridView 更改包装器对象的属性,则需要某种方法将这些更改向下冒泡到您在选择数据时使用的基础 LINQ-to-SQL 对象中。例如,您可以在包装器的属性上使用 setter 来设置底层 LtS 对象的属性。

The most basic way to use LINQ-to-SQL classes, I believe, is to instantiate a list of them (let's use Employee, for example) to contain the Employees you wish to (potentially) edit. When the properties of those Employee objects are changed, the objects are automatically "dirtied", and a call to DataContext.SubmitChanges() will persist them.

List<Employee> employees = (from e in dataContext.Employees where e.Salary > 50000 select e).toList();

foreach(var employee in employees)
{
  employee.CanAffordToyotaPrius = true;
}

dataContext.SubmitChanges();

If you're wrapping the DataContext and only altering the properties of the wrapper object with the DataGridView, you'll need some way to bubble those changes down into the underlying LINQ-to-SQL objects you used when you selected the data. For example, you could use the setter on your wrapper's properties to also set the underlying LtS object's properties.

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