Advertisement
wingman007

TableDataGateway

Nov 11th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.OleDb;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace CRUDDummyAccessConsole3a
  11. {
  12.     class Users
  13.     {
  14.        // IDbConnection aConnection; // the best
  15.         // DbConnection aConnection; // second best
  16.         OleDbConnection aConnection;
  17.  
  18.         string table = "users";
  19.  
  20.         public Users(OleDbConnection aConnection)
  21.         {
  22.             this.aConnection = aConnection;
  23.         }
  24.  
  25.         public OleDbDataReader FetchAll()
  26.         {
  27.             OleDbDataReader aReader = null;
  28.             OleDbCommand aCommand = new OleDbCommand("SELECT * FROM users", aConnection);
  29.             try
  30.             {
  31.                 aConnection.Open();
  32.                 aReader = aCommand.ExecuteReader();
  33.                 Console.WriteLine("This is the returned data from users table");
  34.                 // aReader.Close();
  35.                 // aConnection.Close(); // moved to finally always gets executed
  36.             }
  37.             catch (OleDbException e)
  38.             {
  39.                 Console.WriteLine("Error {0}", e.Errors[0].Message);
  40.             }
  41.             finally
  42.             {
  43.                 aReader.Close();
  44.                 aConnection.Close();
  45.             }
  46.             return aReader;
  47.         }
  48.  
  49.         public int Create() // Insert
  50.         {
  51.             int numberOfRows = 0;
  52.             try
  53.             {
  54.                 aConnection.Open();
  55.                 // SQL Injection
  56.                 OleDbCommand aCommand = new OleDbCommand("INSERT INTO users (username, `password`, email) VALUES ('О''Харра','insertedPass', 'inserted@gmail.com')", aConnection);
  57.                 numberOfRows = aCommand.ExecuteNonQuery();
  58.                 // Console.WriteLine("The number of rows during Insert were {0}", numberOfRows);
  59.                 // aConnection.Close(); // moved to finally
  60.             }
  61.             catch (OleDbException e)
  62.             {
  63.                 Console.WriteLine("Error {0}", e.Errors[0].Message);
  64.             }
  65.             finally
  66.             {
  67.                 aConnection.Close();
  68.             }
  69.             return numberOfRows;
  70.         }
  71.  
  72.         public int Update(int id, string username, string password, string email)
  73.         {
  74.             int numberOfRows = 0;
  75.             try
  76.             {
  77.                 aConnection.Open();
  78.                 // SQL Injection
  79.                 // OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'updatedUsername' WHERE ID = " + ID, aConnection);
  80.                 OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'updatedUsername' WHERE ID = @par1", aConnection);
  81.                 // 1. approach with AddWithValue
  82.                 // aCommand.Parameters.AddWithValue("@par1", ID);
  83.                 // or
  84.                 // 2.
  85.                 aCommand.Parameters.AddRange(new[] {
  86.                     new OleDbParameter("@par1", id)
  87. //                     new OleDbParameter("@par2", username),
  88.                 });
  89.                 numberOfRows = aCommand.ExecuteNonQuery();
  90.                 // Console.WriteLine("The number of rows during Update were {0}", numberOfRows);
  91.                 // aConnection.Close(); // moved to finally
  92.             }
  93.             catch (OleDbException e)
  94.             {
  95.                 Console.WriteLine("Error {0}", e.Errors[0].Message);
  96.             }
  97.             finally
  98.             {
  99.                 aConnection.Close();
  100.             }
  101.             return numberOfRows;
  102.         }
  103.  
  104.         public int Delete(int id)
  105.         {
  106.             int numberOfRows = 0;
  107.             try
  108.             {
  109.                 aConnection.Open();
  110.                 // SQL Injection
  111.                 // OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'updatedUsername' WHERE ID = " + ID, aConnection);
  112.                 OleDbCommand aCommand = new OleDbCommand("DELETE FROM users WHERE ID = @par1", aConnection);
  113.                 // 1. approach with AddWithValue
  114.                 aCommand.Parameters.AddWithValue("@par1", id);
  115.                 // or
  116.                 // 2.
  117.                 //                aCommand.Parameters.AddRange(new[] {
  118.                 //                    new OleDbParameter("@par1", ID)
  119.                 //                     new OleDbParameter("@par2", username),
  120.                 //                });
  121.                 numberOfRows = aCommand.ExecuteNonQuery();
  122.                 Console.WriteLine("The number of rows during Delete were {0}", numberOfRows);
  123.                 // aConnection.Close();
  124.             }
  125.             catch (OleDbException e)
  126.             {
  127.                 Console.WriteLine("Error {0}", e.Errors[0].Message);
  128.             }
  129.             finally
  130.             {
  131.                 aConnection.Close(); // moved to finally
  132.             }
  133.             return numberOfRows;
  134.         }
  135.  
  136.         public OleDbDataReader fetchOneById(int id)
  137.         {
  138.             OleDbDataReader aReader = null;
  139.             OleDbCommand aCommand = new OleDbCommand("SELECT * FROM users WHERE id = @par1", aConnection);
  140.             aCommand.Parameters.AddWithValue("@par1", 1);
  141.             try
  142.             {
  143.                 aConnection.Open();
  144.                 aReader = aCommand.ExecuteReader();
  145.                 Console.WriteLine("This is the returned data from users table");
  146.                 // aReader.Close();
  147.                 // aConnection.Close(); // move do finally
  148.             }
  149.             catch (OleDbException e)
  150.             {
  151.                 Console.WriteLine("Error {0}", e.Errors[0].Message);
  152.             }
  153.             finally
  154.             {
  155.                 aReader.Close();
  156.                 aConnection.Close();
  157.             }
  158.             return aReader;
  159.         }
  160.  
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement