c# - 遍历数据库表的记录集

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

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

如果totalPurchase amount >=200, then 5% disc, 如果它 >=500,那么 7%.. 就像那样。

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

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

我正在尝试实现 recordet 以遍历其字段为..的数据库表 Discount

DiscID -1

DiscPer -5

DiscAmount -200 

等等..

最佳答案

我觉得您缺少用于存储折扣金额范围的数据库列,例如 200700 等。我假设数据库表名称为 折扣 和范围列为 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();

关于c# - 遍历数据库表的记录集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11880646/

相关文章:

.net - 有没有办法将 Setter 元素放入 EventTrigger 中?

.net - 在没有 Mage 的情况下清除 .NET 下载的应用程序缓存?

c# - 如何从源代码 View (aspx) 在 C# 中为来自按钮的事件添加事件处理程序

C# - 通过子类实现的接口(interface)继承和使用时找不到方法的用法

c# - 持久性 Cookie 在浏览器关闭时被删除 - Identity 2.0

c# - Local/Express IIS/External Host 分别带来了什么?

c# - 更新 webservice,而不更新 web.config

c# - 检测阈值区域

c# - ASP.NET 核心 : Custom validation

c# - 将 ASP.Net 网站翻译成另一种语言