Advertisement
wingman007

CRUDDummyMSSQLServerConsole3a

Nov 4th, 2014
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.OleDb;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace CRUDDummyMSSQLServerConsole3a
  10. {
  11.     class Program
  12.     {
  13.         static OleDbConnection aConnection;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             /*
  18.             aConnection =
  19.                 new OleDbConnection("Provider=SQLNCLI11;Data Source=FMI-431-2\\SQLEXPRESS;Password=sa;User ID=sa;Initial Catalog=Users");
  20.  
  21.             Add();
  22.  
  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.                 aConnection.Close();
  44.             }
  45.             */
  46.             // System.Data.SqlClient
  47.             Console.WriteLine("hello");
  48.             string connectionString = "Data Source=FMI-431-2\\SQLEXPRESS;Password=sa;User ID=sa;Initial Catalog=Users";
  49.             SqlConnection conn = new SqlConnection(connectionString);
  50.             conn.Open();
  51.             Console.WriteLine("done");
  52.  
  53.         }
  54.  
  55.         public static void Add()
  56.         {
  57.             try
  58.             {
  59.                 aConnection.Open();
  60.                 OleDbCommand aCommand = new OleDbCommand("INSERT INTO Users (id, username, password, email) VALUES (2, 'rewr','sdsa','my@example.com')", aConnection);
  61.                 // !!! SQL Injections
  62.                 aCommand.ExecuteNonQuery();
  63.                 aConnection.Close();
  64.             }
  65.             catch (OleDbException e)
  66.             {
  67.                 Console.WriteLine("Error: {0}", e.Errors[0].Message);
  68.                 aConnection.Close();
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement