Facebook
From MJ, 4 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 169
  1. uusing Oracle.ManagedDataAccess.Client;
  2. using System;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. namespace Zadanie1
  7. {
  8.     public class Program
  9.     {
  10.         private const string TCP_HOST = "150.254.11.230";
  11.         private const int TCP_PORT = 31084;
  12.         private const string ORACLE_CONNECTION_STRING =
  13.             "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=150.254.11.231)" +
  14.             "(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));" +
  15.             "User ID=egzamin2019;Password=student;";
  16.         private static Random random = new Random();
  17.  
  18.         public static void Main(string[] args)
  19.         {
  20.             Socket client;
  21.             byte[] buffer;
  22.             int a, i;
  23.             OracleConnection connection;
  24.             OracleCommand command;
  25.  
  26.             client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  27.             client.Connect(TCP_HOST, TCP_PORT);
  28.             buffer = new byte[20];
  29.             client.Receive(buffer, 4, SocketFlags.None);
  30.             a = BitConverter.ToInt32(buffer, 0);
  31.             Console.WriteLine("Odebrano A = " + a);
  32.             connection = new OracleConnection(ORACLE_CONNECTION_STRING);
  33.             connection.Open();
  34.  
  35.             for (i = 0; i < a; i++)
  36.             {
  37.                 client.Receive(buffer);
  38.                 Console.Write("Odebrano = ");
  39.                 PrintOutByteArray(buffer);
  40.                 command = new OracleCommand();
  41.                 command.Connection = connection;
  42.                 command.CommandText = string.Format(
  43.                     "INSERT INTO grupaA (ID, indeks, student) VALUES ({0}, 131084, '{1}')",
  44.                     random.Next(1, 1000), Encoding.UTF8.GetString(buffer));
  45.                 command.ExecuteNonQuery();
  46.                 Console.WriteLine("Wykonano instrukcję SQL = " + command.CommandText);
  47.             }
  48.  
  49.             connection.Close();
  50.             client.Close();
  51.         }
  52.  
  53.         private static void PrintOutByteArray(byte[] buffer)
  54.         {
  55.             int i;
  56.  
  57.             for (i = 0; i < buffer.Length; i++)
  58.             {
  59.                 Console.Write(buffer[i]);
  60.             }
  61.  
  62.             Console.WriteLine();
  63.         }
  64.     }
  65. }
  66.