保存前检查项目是否存在
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望实现的效率与数据库的往返次数有关,那么您可以编写一个存储过程来为您执行更新或插入。
然而,在大多数情况下,实际上没有必要首先避免 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.
我通常通过执行以下操作来完成此操作:
或者,如果您创建防止重复行的唯一约束,则可以执行相反的操作:
I usually do this by performing the following:
Alternatively, you can do the opposite, if you create a unique constraint that prevents duplicate rows:
运行选择查询检查 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.