Advertisement
a3f

fetching info from 2 MySQL tables

a3f
May 4th, 2012
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using MySql.Data.MySqlClient;
  5. namespace fatoum.mysql._2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string MyConString = "SERVER=localhost;" +
  12.                 "DATABASE=a9895290_newedu;" +
  13.                 "UID=root;" +
  14.                 "PASSWORD=;";
  15.             MySqlConnection connection = new MySqlConnection(MyConString);
  16.             MySqlCommand command = connection.CreateCommand();
  17.             MySqlDataReader Reader;
  18.             command.CommandText = "select id,name from users where type = 4";
  19.             try
  20.             {
  21.                 connection.Open();
  22.             }
  23.             catch (MySqlException mse)
  24.             {
  25.                 Console.WriteLine(mse.Message);
  26.                 Console.ReadKey();
  27.             }
  28.             Reader = command.ExecuteReader();
  29.             var students = new List<string>();
  30.  
  31.             while (Reader.Read())
  32.             {
  33.                 for (int i = 0; i < Reader.FieldCount; i++)
  34.                     students.Add(Reader.GetValue(i).ToString());
  35.              }
  36.             Reader.Close();
  37.             string[] array1 = students.ToArray();
  38.             string id_str = "";
  39.             for (int i = 0; i < students.Count / 2; i = i + 2)
  40.             {
  41.                 id_str += "`user` = \"" + students[i] + "\" OR ";
  42.             }
  43.  
  44.             Console.WriteLine("Total of {0} ppl with this code", students.Count / 2, "\n");
  45.  
  46.             command.CommandText = "select user,result,unknown from results where (" + id_str + "0=1 ) and exam = 5";
  47.             try
  48.             {
  49.                 Reader = command.ExecuteReader();
  50.             }
  51.             catch
  52.             {
  53.                 Console.Write(command.CommandText);
  54.                 Console.ReadKey();
  55.             }
  56.             int n = 0;
  57.             int gazar = 0;
  58.             var results = new List<string>();
  59.             while (Reader.Read())
  60.             {
  61.                 for (int i = 0; i < Reader.FieldCount; i++)
  62.                 {
  63.                     results.Add(Reader.GetValue(i).ToString());
  64.                 }
  65.                 /* for (int i = 0; i < Reader.FieldCount / 2; i = i + 2)
  66.                  {
  67.                      //Console.WriteLine("{0}: \"{1}\"\t\t!{2} \t ?{3}", array[i - 1], Reader.GetValue(0), array[i], array[i + 1]);
  68.                      try{
  69.                          Console.WriteLine(gazar++ + ":{0})\t{1}\t\t{2},\t left out:{3}", array[n],
  70.                          array[n + 1],
  71.                          Reader.GetValue(i),
  72.                          Reader.GetValue(i + 1));
  73.                      }catch{
  74.                      }
  75.                        
  76.                  } n = n + 2;*/
  77.  
  78.             }
  79.             Console.WriteLine(results.Count);
  80.             string[] array2 = results.ToArray();
  81.             int index,m = 0;
  82.                 string first,second;
  83.                
  84.             for (int i = 0; i < students.Count; i = i + 2)
  85.             {
  86.                 first = second = "n/a";
  87.                 if (Array.FindIndex(array2, t => t.IndexOf(array1[i], StringComparison.InvariantCultureIgnoreCase) >= 0) >= 0)
  88.                 {
  89.                     //StringComparison.InvariantCultureIgnoreCase
  90.                     index = Array.FindIndex(array2, t => t.IndexOf(array1[i], StringComparison.InvariantCultureIgnoreCase) >= 0);
  91.                    // index = Array.IndexOf(array2, array1[i].ToLower());
  92.                     first = array2[index + 1];
  93.                     second = array2[index + 2];
  94.                     ++m;
  95.                 }
  96.                 try
  97.                 {
  98.                     Console.WriteLine((++gazar) + ":{0})\t{1}\t\t{2},\t left out:{3}", array1[i],
  99.                     array1[i + 1],
  100.                     first,
  101.                     second);
  102.                
  103.                 }
  104.                 catch
  105.                 {
  106.                 }
  107.             }
  108.             Console.WriteLine(m);
  109.             Reader.Close();
  110.  
  111.             connection.Close();
  112.             Console.ReadKey();
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement