更新数据库记录的最佳实践

发布于 2024-07-15 05:26:47 字数 214 浏览 5 评论 0原文

假设您检索 100 条记录,并将它们显示在页面上。 用户仅更新了页面上的 2 条记录。 现在您只想更新两条记录,而不更新其他 98 条记录。

是否最好在页面上提交一条记录,然后以某种方式知道更新了哪两条记录,然后仅将这两条记录发送到数据库进行更新?

“以某种方式”是什么样子的?

或者,您是否会为每一行设置一个更新提交按钮,并且仅更新与其关联的记录?

Say you retrieve 100 records, and display them on a page. The user only updates 2 of the records on the page. Now you want to update only the two records, and not the other 98.

Is it best to have one submit on the page, then somehow know which 2 are updated, then send only those two to the db for an update?

What does the "somehow" look like?

Or, would you have an update-submit button for each row, and have it only update the record its tied to?

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

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

发布评论

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

评论(5

烧了回忆取暖 2024-07-22 05:26:47

当然,您可以通过不同的方式来做到这一点。 一般来说,您可以使用 Javascript 只为已更改的记录组装 POST 数据,从而节省一些麻烦和服务器端处理。 关于它如何工作的两个想法:

1)走ajax路线并进行实时编辑。 因此记录显示在表格中并且看起来不可编辑。 当用户单击特定行时,可以使用 Javascript 动态创建适当的 html 表单来编辑该行。 然后有一个提交按钮或一些其他处理程序(例如,将焦点移动到另一个表行),这将触发更新数据库的 POST(通过您首选的 ajax 方法异步)。 令人高兴的是主流 Javascript 框架可以在这个领域提供很大帮助。

2) 复选框 - 每当编辑一行时,其复选框就会被选中。 单击提交按钮时,使用 javascript 通过抓取选中复选框的行中的所有内容来发布 POST 数据。 用户可以在提交之前取消选中复选框以取消对该行的更改。

Of course there are different ways you could do this. In general, you can save yourself some trouble and server-side processing by using Javascript to assemble your POST data for only the records that have changed. Two thoughts on how this might work:

1) Go the ajax route and do live-editing. So records are presented in a table and appear to be non-editable. When a user clicks a particular row, that row becomes editable by using Javascript to create the appropriate html form on the fly. Then have either a submit button or some other handler (say, moving focus to another table row) which will trigger the POST which updates the DB (asynchronously via your preferred ajax method). Happily the mainstream Javascript frameworks can help a lot in this area.

2) Checkboxes - whenever a row is edited, its checkbox becomes checked. When the submit button is clicked, use javascript to post the POST data by grabbing everything in row whose checkbox is checked. A user can un-check a box to cancel changes to that row before submitting.

小糖芽 2024-07-22 05:26:47

使用 jQuery 或其他一些 JavaScript 库进行 Ajax,并在每一行上放置和更新按钮。

Ajax it using jQuery or some other JavaScript library and put and update button on each row.

昔日梦未散 2024-07-22 05:26:47

这个问题有很多答案,在某种程度上它们取决于您的开发工具和网站的“感觉”。

如果您要实现 Ajax 调用来逐行进行更新,那么从逻辑上讲,每行都有一个按钮,然后在行更改时使用 Ajax 调用更新它,这在逻辑上似乎是正确的。

这也正是断开连接的数据集旨在解决的场景,ADO.net 可以很好地处理这些问题。

一如既往,答案是“这取决于情况!”

There are many answers to this question and to some extent they depend upon your development tools and the "feel" of the site.

If you were implementing Ajax calls to do the updates on a line by line basis then this would logically seem right to have a button per line and then update it with an Ajax call when a line was changed.

This is also just the scenario that disconnected data sets were designed to solve and ADO.net handles these very well.

So as ever, the answer is "It Depends!"

你列表最软的妹 2024-07-22 05:26:47

当用户更改输入字段时,您可以使用 JavaScript 将每个字段标记为已更改。 创建一个隐藏字段,其中包含要更新的行的 ID 和脏标志。 (如 is_dirty_$id)在 JavaScript 中,创建一个 onChange 处理程序,将隐藏字段设置为脏字段。 当任何输入改变时。

或者,您可以为显示的每个实际字段创建隐藏字段。 隐藏字段将包含初始值。 检查服务器端的每个字段以确定发生了什么变化。

您可能希望将 last_modified 日期存储为每条记录的隐藏字段。 这样,如果另一个用户更新同一条记录,您可以显示一条错误消息,指出“此记录已被另一个用户更新”或类似信息。

You can use JavaScript to mark each field as changed when a user changes an input field. Create a hidden fields that has the id of the row you are updating, and dirty flag. (like is_dirty_$id) In JavaScript, create an onChange handler that sets the hidden field as dirty. when any input is changed.

Alternatively, you can create hidden fields for each real field you display. the hidden field would contain the initial values. check each field on the server side to determine what has changed.

You probably want to store a last_modified date as a hidden field for each record. This way if another user updates the same record, you can display an error message saying "this record has been updated by another user" or similar.

不气馁 2024-07-22 05:26:47

一个提交按钮。 我可以预见到我可能会使用不止一个,但一般情况下只使用一个。 (注意,这对我来说看起来像是一个网页问题,所以我用这个假设来回答。)

您可以想到 3 种处理跟踪更改的方法:

JavaScript:在控件上放置 onChange() 函数更新隐藏字段。 如果该隐藏值有值,则更新关联的记录。 需要浏览器上的 JS,并且不会告诉您要更新哪些字段,而只是告诉您哪些记录。

许多表单字段:为每个控件放置一个隐藏字段,并在它们返回时对它们进行比较。 这会很难看,但它可以让您知道要更新哪些字段(而不仅仅是记录)。 它还可以让您知道是否有人撤消了已开始的更改。

会话:您可以将原始值放入会话变量中,然后在值返回时进行比较。 这比许多隐藏字段更优雅,并且对使用回传数据的人不太开放(因为你永远不应该相信任何返回的内容,即使在隐藏字段中)。 需要浏览器上的 cookie 和服务器上的会话技术。

One submit button. I could foresee case I might use more then one, but in the general case just one. (Note, this looks like a web page question to me, so I'm answering with that assumption.)

There are 3 ways that come to mind which you could handle the tracking changes:

JavaScript: Put a onChange() function on the controls that update a hidden field. If that hidden has a value, then update the associated record. Requires JS on the browser, and doesn't tell you which fields to update, just which records.

Lots of form fields: Put a hidden field out with each control and compare them all when they come back. This would be ugly, but it would allow you to know which fields to update (not just the record). It would also allow you to know if someone undid a change that started.

Sessions: You could place the original values in session variables, then do the comparison when the values come back. This would be a little more elegant then lots of hidden fields, and less open to people playing with the posted back data (since you should never trust anything that comes back, even in hidden fields). Requires cookies on the browser and sessions on the server technology.

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