How to change this:

public void Charge(string bankaccountnumber, decimal moneyamount)
    {
       foreach (Bankaccount bankaccountInProcess in bankaccount)
        {
            if (bankaccountnumber == bankaccountInProcess.Bankaccountnumber)

                bankaccountInProcess.ChargeFromBankAccount(moneyamount);

        }

        return;
}

To use the OleDbDataReader instead of that foreach?

The moneyamount is inputted in console when asked to input the amount of money to be withdrawn from the bank account and then withdrawn from the BalanceAmountAvailableForWithdrawal (and also from WholeAccountBalance). But there is no moneyamount column in the access file, only WholeAccountBalance and BalanceAmountAvailableForWithdrawal currency columns.

Here is an example how I have changed it in different part of my class:

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;

    }

Here's what I have come up so far:

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;

        }
}
有帮助吗?

解决方案

If your Access table Account has 3 columns: accounNum, WholeAccountBalance and BalanceAmountAvailableForWithdrawal, then you basically should do something like this (I've also corrected and updated it a little). Just check it as I didn't test it.

public void Charge(string bankaccountnumber, decimal moneyamount)
{
    using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\\asd.accdb"))
    {
        connection.Open();

        using (OleDbCommand cmd = new OleDbCommand())
        {
            cmd.Connection = connection;
            cmd.CommandText = "SELECT accounNum, WholeAccountBalance, BalanceAmountAvailableForWithdrawal FROM Account WHERE accounNum = @accNum";
            cmd.Parameters.AddWithValue("@accNum", bankaccountnumber);

            using (OleDbDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())  //assuming accounNum is an unique field (primary key)
                {
                    decimal balanceAmountAvailableForWithdrawal = Convert.ToDecimal(reader["BalanceAmountAvailableForWithdrawal"]);
                    if (balanceAmountAvailableForWithdrawal >= moneyamount)   //check if you can withdraw money
                    {
                        cmd.CommandText = @"UPDATE Account 
                                            SET WholeAccountBalance = WholeAccountBalance - @moneyamount, 
                                                BalanceAmountAvailableForWithdrawal = BalanceAmountAvailableForWithdrawal - @moneyamount 
                                            WHERE accounNum = @accNum";

                        cmd.Parameters.AddWithValue("@moneyamount", moneyamount);

                        int result = cmd.ExecuteNonQuery();

                        if (result == 1)
                            Console.WriteLine("Money withdrawn sucessfully.");
                        else
                            Console.WriteLine("Error while withdrawing, operation has failed.");
                    }
                    else
                    {
                        Console.WriteLine("Not enough money to withdrawn requested ammount.");
                    }
                }
                else
                {
                    Console.WriteLine("Charging did not work");
                }
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top