我有一个 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
等等..
最佳答案
我觉得您缺少用于存储折扣金额范围的数据库列,例如 200
到 700
等。我假设数据库表名称为 折扣
和范围列为 AmountFrom
和 AmountTo
。这是我为您提供的解决方案:
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/