Advertisement
wingman007

CRUDDummyAccessConsole2b

Nov 4th, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.OleDb;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace CRUDDummyAccessConsole2b
  9. {
  10.     class Program
  11.     {
  12.         static OleDbConnection aConnection;
  13.         static void Main(string[] args)
  14.         {
  15.             // 1. connect to the DB
  16.  
  17.            // 1.1. Escape the \ with another \
  18.             aConnection =
  19.                 new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\fmi\\Source\\Repos\\fmiedd\\111121321321_StudentStoayn\\CRUDDummyAccessConsole2b\\CRUDDummyAccessConsole2b\\data\\Users.accdb");
  20.           // or
  21.           // 1.2. Instead of escaping add @ up front of the string
  22. //            aConnection =
  23. //                new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\fmi\Source\Repos\fmiedd\111121321321_StudentStoayn\CRUDDummyAccessConsole2b\CRUDDummyAccessConsole2b\data\Users.accdb");
  24.  
  25.             Add();
  26.             Update(4);
  27.             Delete(7);
  28.  
  29.             // 2. Prepare the SQL statement
  30.             OleDbCommand aCommand = new OleDbCommand("SELECT * from users", aConnection);
  31.  
  32.             // Retriev
  33.             try
  34.             {
  35.                 aConnection.Open();
  36.                 OleDbDataReader aReader = aCommand.ExecuteReader();
  37.                 Console.WriteLine("This is the returned data from users table");
  38.                 while (aReader.Read())
  39.                 {
  40.                     // Console.WriteLine(aReader.GetInt32(0).ToString());
  41.                     // Console.WriteLine(aReader.GetString(1));
  42.                     Console.WriteLine("{0} \t {1}", aReader.GetInt32(0).ToString(), aReader.GetString(1));
  43.                     // or
  44.                     // Console.WriteLine("{0} \t {1}", aReader[0], aReader[1]);
  45.                 }
  46.                 aReader.Close();
  47.                 aConnection.Close();
  48.             }
  49.             catch (OleDbException e)
  50.             {
  51.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  52.             }
  53.         }
  54.  
  55.         // C- Create
  56.         public static void Add()
  57.         {
  58.             try
  59.             {
  60.                 aConnection.Open();
  61.                 // SQL Injections ; DROP TABLE users;
  62.                 // Escape the symbols in the values with '
  63.                 // (e.g O'Harra escape like 'O''Harra'
  64.                 // You don'r have to care for escaping if you do parametrisation e.g. @par1 or
  65.                 // 1. Wrap the fields that look like keywords with `
  66.                 OleDbCommand aCommand = new OleDbCommand("INSERT INTO users (username, `password`, email) VALUES ('insertedUsernameO''Harra', 'insertedPassword', 'insertedEmail@gmail.com')", aConnection);
  67.                // or
  68.                // 2. Wrap the fields that look like keywords with []
  69. //                OleDbCommand aCommand = new OleDbCommand("INSERT INTO users (username, [password], email) VALUES ('insertedUsername', 'insertedPassword', 'insertedEmail@gmail.com')", aConnection);
  70.  
  71.                 int numberOfRows = aCommand.ExecuteNonQuery();
  72.                 aConnection.Close();
  73.                 Console.WriteLine("Number of records affected {0} from Insert", numberOfRows);
  74.             }
  75.             catch (OleDbException e)
  76.             {
  77.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  78.             }            
  79.         }
  80.  
  81.         // Update
  82.         public static void Update(int ID)
  83.         {
  84.             try
  85.             {
  86.                 aConnection.Open();
  87.                 // OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'updatedUsername' WHERE ID = " + ID, aConnection);
  88.                 OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'updatedUsername' WHERE ID = @par1", aConnection);
  89.                 // 1. approach with AddWithValue
  90.                 // aCommand.Parameters.AddWithValue("@par1", ID);
  91.  
  92.                 // 2. approach with AddRange
  93.                 aCommand.Parameters.AddRange(new[] {
  94.                     new OleDbParameter("@par1", ID)
  95.                 });
  96.                 int numberOfRows = aCommand.ExecuteNonQuery();
  97.                 aConnection.Close();
  98.                 Console.WriteLine("Number of records affected {0} from Update", numberOfRows);
  99.             }
  100.             catch (OleDbException e)
  101.             {
  102.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  103.             }        
  104.         }
  105.  
  106.         // D - Delete
  107.         public static void Delete(int ID)
  108.         {
  109.             try
  110.             {
  111.                 aConnection.Open();
  112.                 OleDbCommand aCommand = new OleDbCommand("DELETE FROM users WHERE ID = @par1", aConnection);
  113.                 // aCommand.Parameters.AddWithValue("@par1", ID);
  114.                 aCommand.Parameters.AddRange(new[] {
  115.                     new OleDbParameter("@par1", ID)
  116.                 });
  117.                 int numberOfRows = aCommand.ExecuteNonQuery();
  118.                 aConnection.Close();
  119.                 Console.WriteLine("Number of records affected {0} from Delete", numberOfRows);
  120.             }
  121.             catch (OleDbException e)
  122.             {
  123.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  124.             }        
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement