gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:56:54 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com