【问题标题】:Recordset to iterate through database tableRecordset 遍历数据库表
【发布时间】:2012-08-09 09:45:44
【问题描述】:

我有一个 asp.net 电子商务应用程序,我在其中添加了折扣功能:

如果totalPurchase amount >=200, then 5% disc, 如果是is >=500, then 7%.. 那样的话。

问题是折扣百分比应该通过管理员登录动态更改。即我不允许在代码隐藏中编写此代码。

if(totalPurchase>=200 && totalPurchase<700)
{ // code for 5% discount }

我正在尝试实现 recordet 来遍历数据库表 Discount,其字段是..

DiscID -1

DiscPer -5

DiscAmount -200 

等等……

【问题讨论】:

  • 我不明白这部分问题问题是折扣百分比应该通过管理员登录动态更改。即我不允许写
  • 管理员可以动态更改折扣百分比,折扣金额。意味着代码(后端)不需要每次都更改。
  • 还应该有第三列来说明此折扣应适用的限制。示例:200+ 的 5%
  • 是的!我已经考虑过了。但我真正需要的是编写记录集功能的代码隐藏,这将有助于我的方案。谢谢,顺便说一句!
  • 至少告诉我我走对了吗?

标签: c# asp.net .net sql-server sql-server-2008-r2


【解决方案1】:

我觉得您缺少用于存储折扣金额范围的数据库列,例如 200700 等。我假设数据库表名称为 Discounts,范围列为 AmountFromAmountTo。这是我的解决方案:

OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Data.accdb;Persist Security Info=False;";
connect.Open();

OleDbCommand command = new OleDbCommand();
command.CommandText = "SELECT AmountFrom, AmountTo, DiscPer FROM Discounts";
command.Connection = connect;

OleDbReader reader = command.ExecuteReader();

while(reader.Read())
{   
    int from = int.Parse(reader['AmountFrom'].toString());
    int to = int.Parse(reader['AmountTo'].toString());
    int discount = int.Parse(reader['DiscPer'].toString());

    if(totalPurchase >= from && totalPurchase < to) {
        MessageBox.Show("You got a discount of " + discount + "%");
    }
}
connect.Close();

【讨论】:

  • 感谢您的宝贵时间。但是我在我的问题中明确提到我不想写 * if(totalPurchase>=200 && totalPurchase
  • 好的。这是提示..我想实现什么... SqlConnection conn = new SqlConnection(); conn.ConnectionString = "";记录集 rs = new Recordset(); if(totalPurchase>=rs(1)) { // 5% 折扣码 } else if(totalPurchase>=rs(1)) { //7% 折扣码 } else if(totalPurchase>=rs(1)) { / /so on.. } 如何实现这个?我真的不知道使用记录集。请帮忙!
【解决方案2】:

这个 SO 问题可能会帮助您解决折扣问题Database Design Brainstorming: Sale Prices

【讨论】:

  • 刚刚在 Google 上发现 asp.net 不支持记录集。我将使用您的代码或使用数据集。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多