Advertisement
wingman007

CRUDDummyAccess1a

Nov 3rd, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 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 CRUDDummyAccessConsole
  9. {
  10.     class Program
  11.     {
  12.         static OleDbConnection aConnection;
  13.         static void Main(string[] args)
  14.         {
  15.             // 1) establish connection
  16.             aConnection =
  17.                 new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\fmi\\Source\\Repos\\abc\\321312321_StoyanCheresharov\\Task1\\CRUDDummyAccessConsole\\CRUDDummyAccessConsole\\data\\Users.accdb");
  18.            
  19.             Add();
  20.             Update(4);
  21.             Delete(7);
  22.             // 2) prepare the SQL statement
  23.             OleDbCommand aCommand = new OleDbCommand("SELECT * from users", aConnection);
  24.             try
  25.             {
  26.                 aConnection.Open();
  27.  
  28.                 OleDbDataReader aReader = aCommand.ExecuteReader();
  29.                 Console.WriteLine("This is the returned data from emp_test table");
  30.                 while (aReader.Read())
  31.                 {
  32.                     // Console.WriteLine(aReader.GetInt32(0).ToString());
  33.                     // Console.WriteLine(aReader.GetString(1));
  34.                     Console.WriteLine("{0} \t {1}", aReader.GetInt32(0).ToString(), aReader.GetString(1));
  35.                 }
  36.  
  37.                 aReader.Close();
  38.                 aConnection.Close();
  39.             }
  40.             catch (OleDbException e)
  41.             {
  42.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  43.             }
  44.         }
  45.  
  46.         public static void Add()
  47.         {
  48.             try
  49.             {
  50.                 aConnection.Open();
  51.                 OleDbCommand aCommand = new OleDbCommand("INSERT INTO Users (username,`password`,email) VALUES ('rewr','sdsa','my@example.com')", aConnection);
  52.                 // !!! SQL Injections
  53.                 aCommand.ExecuteNonQuery();
  54.                 aConnection.Close();
  55.             }
  56.             catch (OleDbException e)
  57.             {
  58.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  59.             }
  60.         }
  61.  
  62.         public static void Update(int ID)
  63.         {
  64.             try
  65.             {
  66.                 aConnection.Open();
  67.                 // SQL Injections etc.
  68.                 // OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'upadated' WHERE ID = " + ID.ToString(), aConnection);
  69.                 OleDbCommand aCommand = new OleDbCommand("UPDATE users SET username = 'upadated' WHERE ID = @par1", aConnection);
  70.                 // 1) approach with AddWithValue
  71.                 // aCommand.Parameters.AddWithValue("@par1", ID);
  72.  
  73.                 // 2) approach with AddRange
  74.                 aCommand.Parameters.AddRange(new[] {
  75.                     new OleDbParameter("@par1", ID)
  76.                 });
  77.                 aCommand.ExecuteNonQuery();
  78.                 aConnection.Close();
  79.             }
  80.             catch (OleDbException e)
  81.             {
  82.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  83.             }    
  84.         }
  85.  
  86.         public static void Delete(int ID)
  87.         {
  88.             try
  89.             {
  90.                 aConnection.Open();
  91.                 OleDbCommand aCommand = new OleDbCommand("DELETE FROM users WHERE ID = " + ID, aConnection);
  92.                 aCommand.ExecuteNonQuery();
  93.                 aConnection.Close();
  94.             }
  95.             catch (OleDbException e)
  96.             {
  97.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  98.             }
  99.         }
  100.     }
  101. }
  102. /*
  103.          public static void Update(int ID)
  104.         {
  105.             try
  106.             {
  107.                 aConnection.Open();
  108.                 //            OleDbCommand aCommand = new OleDbCommand("INSERT INTO Users (ID, username, password, email) VALUES(3, 'Test', 'testpass', 'testemail')", aConnection);
  109.                 //- OleDbCommand aCommand = new OleDbCommand("INSERT INTO Users (username, password, email) VALUES('Test', 'testpass', 'testemail')", aConnection);
  110.  
  111.                // OleDbCommand aCommand = new OleDbCommand("UPDATE Users SET username='rewrUpdated' WHERE ID=3", aConnection);
  112.                 OleDbCommand aCommand = new OleDbCommand("UPDATE Users SET username='rewrUpdated' WHERE ID=@par1", aConnection);
  113.                 // 1)
  114.                 //- aCommand.Parameters.AddWithValue("@par1", ID);
  115.                 // or
  116.                 // 2)
  117.                 aCommand.Parameters.AddRange(new[] {
  118.                     new OleDbParameter("@par1", ID)
  119.                 });
  120.  
  121.                 int afectedRows = aCommand.ExecuteNonQuery();
  122.                 aConnection.Close();
  123.             }
  124.             catch (OleDbException e)
  125.             {
  126.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  127.             }
  128.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement