如何改变这个:
public void Charge(string bankaccountnumber, decimal moneyamount)
{
foreach (Bankaccount bankaccountInProcess in bankaccount)
{
if (bankaccountnumber == bankaccountInProcess.Bankaccountnumber)
bankaccountInProcess.ChargeFromBankAccount(moneyamount);
}
return;
}
要使用 OleDbDataReader 而不是那个 foreach?
当要求输入要从银行帐户中提取的金额然后从 BalanceAmountAvailableForWithdrawal(以及 WholeAccountBalance)中提取时,在控制台中输入金额。但是access文件中没有moneyamount列,只有WholeAccountBalance和BalanceAmountAvailableForWithdrawal货币列。
这是一个示例,我如何在课堂的不同部分对其进行更改:
public bool IdentifyUser(string bankAccountnumber, int passWord)
{
connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\\asd.accdb");
connection.Open();
OleDbCommand getOne = new OleDbCommand();
getOne.Connection = connection;
getOne.CommandText = "SELECT accounNum, pinCode FROM Account WHERE accountNum = '" +
bankAccountnumber +"'";
OleDbDataReader readOne = readOne.ExecuteReader();
if (readOne.Read())
{
int GottenPassWord = (int)readOne["pinCode"];
if (GottenPassWord == passWord)
return true;
}
else
{
Console.WriteLine("Account was not found");
}
connection.Close();
return false;
}
到目前为止,这是我提出的:
public void Charge(string bankaccountnumber, decimal moneyamount)
{
connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\\asd.accdb");
connection.Open();
OleDbCommand getOne = new OleDbCommand();
getOne.Connection = connection;
getOne.CommandText = "SELECT accounNum, WholeAccountBalance, BalanceAmountAvailableForWithdrawal FROM Account WHERE accounNum = '" +
bankaccountnumber + "'";
OleDbDataReader readOne = readOne.ExecuteReader();
if (readOne.Read())
{
// i dont know what to put here to make this do the same thing as the foreach did
return;
}
else
{
Console.WriteLine("Charging did not work");
return;
}
}