Advertisement
Guest User

Untitled

a guest
May 30th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. using Oracle.DataAccess.Client;
  8.  
  9.  
  10. namespace BD
  11. {
  12.     class Program
  13.     {
  14.         public static OracleConnection RunOracle()
  15.         {
  16.             OracleConnection conn = new OracleConnection();
  17.             String oradb = "Data Source=(DESCRIPTION=" +
  18.                 "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=194.29.178.137)(PORT=1521)))"
  19.                 + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));"
  20.                 + "User Id=TEST3;Password=STUDENT;";
  21.             try
  22.             {
  23.                 conn = new OracleConnection(oradb);
  24.                 conn.Open();
  25.                 Console.WriteLine("Ustanowiono połączenie z bazą danych");
  26.             }
  27.             catch (Exception ex)
  28.             {
  29.                 Console.WriteLine("{0}: Nie udało się połączyć", ex.Source);
  30.             }
  31.             return conn;
  32.         }
  33.  
  34.         private static string GetSelectFromUser()
  35.         {
  36.             Console.WriteLine("\n\nPodaj select do wykonania:");
  37.             return Console.ReadLine();
  38.         }
  39.  
  40.         private static void ExecuteSelect(IDbConnection con, string str)
  41.         {
  42.             IDbCommand com = con.CreateCommand();
  43.             com.CommandType = CommandType.Text;
  44.             com.CommandText = str;
  45.             AddRow(con, str);
  46.             try
  47.             {
  48.                 using (IDataReader reader = com.ExecuteReader())
  49.                 {
  50.                     int i = 0;
  51.                     while (reader.Read())
  52.                     {
  53.                         Console.WriteLine("\n" + reader[0] +"\n" + reader[1] + "\n" + reader[2] + "\n" + reader[3] + "\n\n----------------------------------------------------");
  54.                         i++;
  55.                     }
  56.                 }
  57.             }
  58.             catch (Exception ex)
  59.             {
  60.                 Console.WriteLine("\nERROR\n{0}\n", ex.Message);
  61.             }
  62.    
  63.         }
  64.  
  65.         private static void AddRow(IDbConnection con, string query)
  66.         {
  67.             IDbCommand com = con.CreateCommand();
  68.             com.CommandType = CommandType.Text;
  69.             string user = "194.29.178.147";
  70.             try
  71.             {
  72.                 string str = string.Format(@"INSERT INTO Qlog (QID,QDATE,QUERY,QUSER) VALUES (QLOG_SEQ.NEXTVAL,CURRENT_DATE,'{0}','{1}')", query, user);
  73.                 com.CommandText = str;
  74.                 com.ExecuteNonQuery();
  75.             }
  76.             catch (Exception ex)
  77.             {
  78.                 Console.WriteLine("\nERROR\n{0}\n",ex.Message);
  79.             }
  80.         }
  81.         static void Main(string[] args)
  82.         {
  83.             OracleConnection con = RunOracle();
  84.             while (true)
  85.             {
  86.                 string str = GetSelectFromUser();
  87.                 ExecuteSelect(con, str);
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement