更新 ADO 记录集中的字段并重新排序经典 ASP 中的值

发布于 2024-09-06 02:29:29 字数 937 浏览 10 评论 0原文

目前的情况是这样的:我有一个产品记录集。这些产品的价格取决于页面其他地方的其他信息。因此,当我循环输出记录集时,我使用函数计算价格并显示它。这一切都非常有效。

现在,针对新的皱纹。在显示产品之前,我需要能够按计算的价格对产品进行排序。我的尝试是简单地向记录集添加一个新字段,循环一次记录集以计算价格并更新该新字段,然后在再次循环显示之前在该字段上使用记录集。

不用说,这是行不通的。无论我对此记录集设置什么设置(lockType、cursorType、cursorLocation),都会出现一条错误消息,指出“当前记录集不支持更新。这可能是提供程序或所选锁定类型的限制。”

有什么方法可以使这项工作如所描述的那样,或者有其他方法可以实现这个结果吗?作为参考,这里是我当前正在使用的代码片段。

set rsProd = server.CreateObject("adodb.recordset")
rsProd.cursorType = adOpenKeyset
rsProd.lockType = adLockPessimistic
rsProd.cursorLocation = adUseServer
rsProd.Open sql, Conn1

'loop through the records and calculate prices'
Do until rsProd.EOF  
   laPrices = Split(GetParameterProductPrice(rsProd("Product_ID")), "|")
   'ERROR OCCURS ON NEXT LINE'  
   rsProd("Sale") = CDbl(laPrices(0))  
   rsProd("Sale_Special")= CDbl(laPrices(1))  
   rsProd.MoveNext
Loop
rsProd.MoveFirst
rsProd.Sort = "Sale_Special, Sale, Product_Code"

Here's the current situation: I have a recordset of products. The price of these products depends upon other information elsewhere on the page. So, as I loop through the recordset for output, I calculate the price using a function, and display it. This all works wonderfully.

Now, for the new wrinkle. I need to be able to sort the products by the calculated price before I display them. My attempt was to simply add a new field to the recordset, loop through the recordset once to calculate the price and update that new field, and then resort the recordset on that field before looping again for the display.

Needless to say, this does not work. No matter what settings (lockType, cursorType, cursorLocation) I put on this recordset, I am presented with an error stating "Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype."

Is there any way to make this work as described, or is there another method of accomplishing this result? For reference, here is the code snippet that I am currently working with.

set rsProd = server.CreateObject("adodb.recordset")
rsProd.cursorType = adOpenKeyset
rsProd.lockType = adLockPessimistic
rsProd.cursorLocation = adUseServer
rsProd.Open sql, Conn1

'loop through the records and calculate prices'
Do until rsProd.EOF  
   laPrices = Split(GetParameterProductPrice(rsProd("Product_ID")), "|")
   'ERROR OCCURS ON NEXT LINE'  
   rsProd("Sale") = CDbl(laPrices(0))  
   rsProd("Sale_Special")= CDbl(laPrices(1))  
   rsProd.MoveNext
Loop
rsProd.MoveFirst
rsProd.Sort = "Sale_Special, Sale, Product_Code"

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

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

发布评论

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

评论(2

客…行舟 2024-09-13 02:29:29

实现此目的的一种方法是在数据库中添加计算列

主要好处是您不需要更改页面。此外,您可以在应用程序的其他地方重复使用计算出的价格 - 我猜您实际上是在网站的其他部分再次计算了价格。

One way to accomplish this would be a computed column in your database.

The main benefit would be that you wouldn't need to change your page(s). Additionally you could reuse the calculated price in other places in your application - I guess you actually calculate the price again on other parts of the site.

暮年 2024-09-13 02:29:29

如果这个记录集不是很大,一种解决方案是在循环期间,将计算出的价格存储到数组中。计算出定价后,您可以使用排序函数对数组进行排序,然后打印结果。

If this recordset is not huge, one solution is during your loop, store your calculated price into an array. Once you've calculated the pricing, you can sort the array with a sorting function and then print out the results.

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