保存前检查项目是否存在

发布于 2024-10-15 23:59:55 字数 426 浏览 12 评论 0原文

我有一个 SQL 数据库,其中包含各种表,用于保存有关产品的信息(用于在线商店),并且我正在使用 C# 进行编码。有一些与给定产品相关的选项,如上所述,记录的有关这些选项的信息在保存时分布在几个表中。

现在,当我在 CMS 中编辑此产品时,我会看到现有产品选项的列表,我可以按照您的预期添加到该列表或从中删除。

当我保存产品时,我需要检查记录是否已存在,如果存在则更新它,如果不存在则保存新记录。我正在尝试找到一种有效的方法来做到这一点。维护与产品选项关联的 ID 非常重要,因此每次都清除它们并重新保存它们是不可行的。

再次描述,可能更清楚:想象一下,当我加载产品时,我有一组选项,这些选项被加载到内存中,并根据用户的选择添加/删除。当他们单击“保存”时,我需要检查哪些选项是更新的以及哪些选项是列表中的新选项。

有什么建议可以有效地做到这一点吗?

谢谢。

I've a SQL DB with various tables that save info about a product (it's for an online shop) and I'm coding in C#. There are options associated with a given product and as mentioned the info recorded about these options is spread across a few tables when saved.

Now when I come to edit this product in the CMS I see a list of the existing product options and I can add to that list or delete from it, as you'd expect.

When I save the product I need to check if the record already exists and if so update it, if not then save a new record. I'm trying to find an efficient way of doing this. It's very important that I maintain the ID's associated with the product options so clearing them all out each time and re-saving them isn't viable unfortunately.

To describe again, possibly more clearly: Imagine I have a collection of options when I load the product, this is loaded into memory and added to / deleted from depending on what the user chooses. When they click 'Save' I need to check what options are updates and what ones are new to the list.

Any suggestions of an efficient way of doing this?

Thanks.

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

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

发布评论

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

评论(3

星光不落少年眉 2024-10-22 23:59:55

如果您希望实现的效率与数据库的往返次数有关,那么您可以编写一个存储过程来为您执行更新或插入。

然而,在大多数情况下,实际上没有必要首先避免 SELECT,只要您的表上有适当的主键或唯一索引,这应该会非常快。

如果效率是指服务器端的优雅或减少的代码,那么我会考虑使用某种 ORM,例如 Entity Framework 4.0。有了正确的 ORM 架构,您几乎可以不再考虑数据库记录和 INSERT/UPDATE,而只需处理内存中的对象集合。

If the efficiency you are looking to achieve is in relation to the number of round trips to the database then you could write a stored procedure to do the update or insert for you.

In most cases however it's not really necessary to avoid the SELECT first, provided you have appropriate primary keys or unique indices on your tables this should be very quick.

If the efficiency is in terms of elegant or reduced code on the server side then I would look at using some sort of ORM, for example Entity Framework 4.0. With a proper ORM architecture you can almost stop thinking in terms of the database records and INSERT/UPDATE and just work with a collection of objects in memory.

月牙弯弯 2024-10-22 23:59:55

我通常通过执行以下操作来完成此操作:

  1. 对于每个项目,执行更新查询,该查询将更新该项目(如果存在)。
  2. 每次更新后,检查更新了多少行(在 SQL Server 中使用 @@ROWCOUNT)。如果更新了零行,则执行插入来创建该行。

或者,如果您创建防止重复行的唯一约束,则可以执行相反的操作:

  1. 对于每个项目,尝试插入它。
  2. 如果插入由于约束而失败(检查错误代码),请改为执行更新。

I usually do this by performing the following:

  1. For each item, execute an update query that will update the item if it exists.
  2. After each update, check how many rows were updated (using @@ROWCOUNT in SQL Server). If zero rows were updated, execute an insert to create the row.

Alternatively, you can do the opposite, if you create a unique constraint that prevents duplicate rows:

  1. For each item, try to insert it.
  2. If the insert fails because of theconstraint (check the error code), perform the update instead.
鸠魁 2024-10-22 23:59:55

运行选择查询检查 ID。如果存在则需要更新。如果不存在则需要插入。

如果没有更多细节,我真的不知道还能告诉你什么。这是相当标准的。

Run a select query checking for the ID. If it exists then you need to update. If it does not exist then you need to insert.

Without more details I'm not really sure what else to tell you. This is fairly standard.

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