Advertisement
Guest User

Untitled

a guest
Dec 4th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data;
  7. using MySql.Data.MySqlClient;
  8. using System.IO;
  9.  
  10. namespace feladat1127
  11. {
  12. class Program
  13. {
  14. public struct diak
  15. {
  16. public string azonosito, nev, telszam;
  17. }
  18.  
  19. static public diak[] diakok = new diak[200];
  20.  
  21. static public MySqlConnection conn = new MySqlConnection("server=localhost;user=root;database=tanulok;port=3306;password=");
  22.  
  23. static public void adatbevitel1()
  24. {
  25. MySqlCommand query = new MySqlCommand
  26. ("insert into 10aosztaly(id,name,phone) values(122,'Jozsi',222)", conn);
  27. try
  28. {
  29. query.ExecuteNonQuery();
  30. Console.WriteLine("Record inserted successfull...");
  31. }
  32. catch (Exception)
  33. {
  34. Console.WriteLine("Query problem");
  35. }
  36. }
  37.  
  38. static public void adatbevitel2()
  39. {
  40. string name, id, phone, q;
  41. Console.Write("id: ");
  42. id = Console.ReadLine();
  43. Console.Write("\nname: ");
  44. name = Console.ReadLine();
  45. Console.Write("\nphone: ");
  46. phone = Console.ReadLine();
  47. q = "insert into 10aosztaly(id,name,phone) values(" + id + ",'" + name + "'," + phone + ")";
  48. MySqlCommand query = new MySqlCommand(q, conn);
  49. try
  50. {
  51. query.ExecuteNonQuery();
  52. Console.WriteLine("Record inserted successfull...");
  53. }
  54. catch (Exception)
  55. {
  56. Console.WriteLine("Query problem");
  57. }
  58. }
  59.  
  60. static public void adatbevitel3()
  61. {
  62. StreamReader sr = new StreamReader("igen.txt");
  63. int i, n;
  64. string q;
  65. string[] adottsor = new string[3];
  66. n = 0;
  67.  
  68. do
  69. {
  70. adottsor = (sr.ReadLine()).Split(' ');
  71. diakok[n].azonosito = adottsor[0];
  72. diakok[n].nev = adottsor[1];
  73. diakok[n].telszam = adottsor[2];
  74. n++;
  75. } while (!sr.EndOfStream);
  76. sr.Close();
  77. for (i = 0; i < n; i++)
  78. {
  79. q = "insert into 10aosztaly(id,name,phone) values(" + diakok[i].azonosito + ",'" + diakok[i].nev + "'," + diakok[i].telszam + ")";
  80. MySqlCommand query = new MySqlCommand(q, conn);
  81. try
  82. {
  83. query.ExecuteNonQuery();
  84. Console.WriteLine("Record inserted successfull...");
  85. }
  86. catch (Exception)
  87. {
  88. Console.WriteLine("Query problem");
  89. }
  90. }
  91.  
  92. }
  93.  
  94. static public void lekerdezes1()
  95. {
  96. StreamWriter sw = new StreamWriter("tabladatok.txt");
  97. MySqlCommand query = new MySqlCommand("select * from 10aosztaly", conn);
  98.  
  99. try
  100. {
  101. MySqlDataReader table = query.ExecuteReader();
  102. while (table.Read())
  103. {
  104. Console.WriteLine(table[0] + " " + table[1] + " " + table[2]);
  105. sw.WriteLine(table[0] + " " + table[1] + " " + table[2]);
  106. }
  107. table.Close();
  108. sw.Close();
  109. }
  110. catch (Exception)
  111. {
  112. Console.WriteLine("Query problem");
  113. }
  114. }
  115.  
  116. static public void torles()
  117. {
  118. MySqlCommand query = new MySqlCommand("delete from 10aosztaly where id=10");
  119.  
  120. try
  121. {
  122. query.ExecuteNonQuery();
  123. Console.WriteLine("Record deleted succesful!");
  124. }
  125. catch(Exception)
  126. {
  127. Console.WriteLine("Query problem!");
  128. }
  129. }
  130.  
  131. static void Main(string[] args)
  132. {
  133. try
  134. {
  135. conn.Open();
  136. Console.WriteLine("Connection to tanulok database successfull!");
  137. //adatbevitel1();
  138. //adatbevitel2();
  139. //adatbevitel3();
  140. lekerdezes1();
  141. torles();
  142. }
  143. catch (Exception)
  144. {
  145. Console.WriteLine("Connection problem!");
  146. }
  147.  
  148. Console.WriteLine();
  149. Console.ReadKey();
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement