Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 4.03 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C# OleDbDataReader
  2. public void Charge(string bankaccountnumber, decimal moneyamount)
  3.     {
  4.        foreach (Bankaccount bankaccountInProcess in bankaccount)
  5.         {
  6.             if (bankaccountnumber == bankaccountInProcess.Bankaccountnumber)
  7.  
  8.                 bankaccountInProcess.ChargeFromBankAccount(moneyamount);
  9.  
  10.         }
  11.  
  12.         return;
  13. }
  14.        
  15. public bool IdentifyUser(string bankAccountnumber, int passWord)
  16.     {
  17.  
  18.  
  19.         connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\asd.accdb");
  20.             connection.Open();
  21.  
  22.  
  23.         OleDbCommand getOne = new OleDbCommand();
  24.         getOne.Connection = connection;
  25.         getOne.CommandText = "SELECT accounNum, pinCode FROM Account WHERE accountNum = '" +
  26.              bankAccountnumber +"'";
  27.         OleDbDataReader readOne = readOne.ExecuteReader();
  28.         if (readOne.Read())
  29.         {
  30.             int GottenPassWord = (int)readOne["pinCode"];
  31.             if (GottenPassWord == passWord)
  32.  
  33.             return true;
  34.         }
  35.  
  36.         else
  37.         {
  38.             Console.WriteLine("Account was not found");            
  39.         }
  40.         connection.Close();
  41.         return false;
  42.  
  43.     }
  44.        
  45. public void Charge(string bankaccountnumber, decimal moneyamount)
  46.     {
  47.  
  48.         connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\asd.accdb");
  49.             connection.Open();
  50.  
  51.         OleDbCommand getOne = new OleDbCommand();
  52.         getOne.Connection = connection;
  53.         getOne.CommandText = "SELECT accounNum, WholeAccountBalance, BalanceAmountAvailableForWithdrawal FROM Account WHERE accounNum = '" +
  54.              bankaccountnumber + "'";
  55.         OleDbDataReader readOne = readOne.ExecuteReader();
  56.         if (readOne.Read())
  57.         {
  58.         // i dont know what to put here to make this do the same thing as the foreach did
  59.  
  60.         return;
  61.  
  62.         }
  63.  
  64.         else
  65.         {
  66.             Console.WriteLine("Charging did not work");
  67.  
  68.             return;
  69.  
  70.         }
  71. }
  72.        
  73. public void Charge(string bankaccountnumber, decimal moneyamount)
  74. {
  75.     using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\asd.accdb"))
  76.     {
  77.         connection.Open();
  78.  
  79.         using (OleDbCommand cmd = new OleDbCommand())
  80.         {
  81.             cmd.Connection = connection;
  82.             cmd.CommandText = "SELECT accounNum, WholeAccountBalance, BalanceAmountAvailableForWithdrawal FROM Account WHERE accounNum = @accNum";
  83.             cmd.Parameters.AddWithValue("@accNum", bankaccountnumber);
  84.  
  85.             using (OleDbDataReader reader = cmd.ExecuteReader())
  86.             {
  87.                 if (reader.Read())  //assuming accounNum is an unique field (primary key)
  88.                 {
  89.                     decimal balanceAmountAvailableForWithdrawal = Convert.ToDecimal(reader["BalanceAmountAvailableForWithdrawal"]);
  90.                     if (balanceAmountAvailableForWithdrawal >= moneyamount)   //check if you can withdraw money
  91.                     {
  92.                         cmd.CommandText = @"UPDATE Account
  93.                                             SET WholeAccountBalance = WholeAccountBalance - @moneyamount,
  94.                                                 BalanceAmountAvailableForWithdrawal = BalanceAmountAvailableForWithdrawal - @moneyamount
  95.                                             WHERE accounNum = @accNum";
  96.  
  97.                         cmd.Parameters.AddWithValue("@moneyamount", moneyamount);
  98.  
  99.                         int result = cmd.ExecuteNonQuery();
  100.  
  101.                         if (result == 1)
  102.                             Console.WriteLine("Money withdrawn sucessfully.");
  103.                         else
  104.                             Console.WriteLine("Error while withdrawing, operation has failed.");
  105.                     }
  106.                     else
  107.                     {
  108.                         Console.WriteLine("Not enough money to withdrawn requested ammount.");
  109.                     }
  110.                 }
  111.                 else
  112.                 {
  113.                     Console.WriteLine("Charging did not work");
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }