Posted By

Vejfil on 05/13/18


Tagged

studenti


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Vejfil


studenti c sharp


 / Published in: C#
 

studenti c sharp

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SQLite;
  7.  
  8. namespace Univerzita
  9. {
  10. class Connection
  11. {
  12. public void Vypiszapis(int psat, string radek, string tabulka)
  13. {
  14. string Univ = "Data Source = univerzita.db; Version = 3";
  15. SQLiteConnection dbconn = new SQLiteConnection(Univ);
  16. dbconn.Open();
  17. string query = "SELECT " + radek + " FROM " + tabulka;
  18. // Console.WriteLine(query);
  19. SQLiteCommand command = new SQLiteCommand(query, dbconn);
  20. SQLiteDataReader reader = command.ExecuteReader();
  21. if (psat == 0)
  22. {
  23. // SQLiteDataReader reader = command.ExecuteReader();
  24. while (reader.Read())
  25. Console.WriteLine(reader[radek]);
  26. }
  27. else if (psat == 1)
  28. {
  29. string pridani = Console.ReadLine();
  30. SQLiteCommand commandins = new SQLiteCommand(query, dbconn);
  31. commandins.CommandText = string.Format("INSERT INTO " + tabulka + "(" + radek + ", kredity) VALUES ('{0}', 0)", pridani);
  32. commandins.ExecuteNonQuery();
  33. // command.CommandText = string.Format("INSERT INTO " + tabulka + "(ID) VALUES (0)", pridani);
  34. Console.WriteLine("nový/zm?n?ný záznam");
  35. Console.WriteLine("-" + pridani);
  36.  
  37. }
  38.  
  39. else if (psat == 2)
  40. {
  41. string name = Console.ReadLine();
  42. Console.WriteLine("zadejte kolik kredit? chcete p?idat/odebrat");
  43. int.TryParse(Console.ReadLine(), out int kredit);
  44. Console.WriteLine("");
  45. string soucasnekredity = "SELECT kredity FROM " + tabulka + " WHERE jmeno='" + name + "'";
  46. SQLiteCommand commandpoc = new SQLiteCommand(soucasnekredity, dbconn);
  47. // Int32 kredits = (Int32)commandpoc.ExecuteScalar();
  48. string stringkred = commandpoc.ExecuteScalar().ToString();
  49. int kredits = Convert.ToInt32(stringkred);
  50. int finkred = kredits + kredit;
  51. string query2 = "UPDATE " + tabulka + " SET kredity=" + finkred +
  52. " WHERE jmeno='" + name + "'";
  53. SQLiteCommand commandkred = new SQLiteCommand(query2, dbconn);
  54. SQLiteDataReader reader2 = commandkred.ExecuteReader();
  55. // Console.WriteLine("kredity byly úsp?šn? p?idány/odebrány");
  56. if (finkred < 0)
  57. {
  58. query2 = "DELETE FROM " + tabulka + " WHERE jmeno='" + name + "'";
  59. commandkred = new SQLiteCommand(query2, dbconn);
  60. commandkred.ExecuteNonQuery();
  61. Console.WriteLine("student dosáhl 0 kredit? byl vyhozen");
  62. }
  63. else { Console.WriteLine("kredity byly úsp?šn? p?idány/odebrány"); }
  64.  
  65.  
  66. }
  67.  
  68.  
  69. else if (psat == 3)
  70. { string query3 = "SELECT * FROM " + tabulka + " ORDER BY kredity";
  71. SQLiteCommand commandall = new SQLiteCommand(query3, dbconn);
  72. reader = commandall.ExecuteReader();
  73. Console.WriteLine("ID Jmeno Kredity");
  74. while (reader.Read())
  75. Console.WriteLine("{0} {1} {2}", reader["ID"], reader["jmeno"], reader["kredity"]);
  76.  
  77. //Console.WriteLine(reader.);
  78. }
  79.  
  80. else if (psat == 4)
  81. {
  82. string query4 = "SELECT Skupina, Count(*), Sum(kredity) FROM Skupiny, Studenti WHERE ID = Studenti GROUP BY Skupina";
  83. SQLiteCommand commandskup = new SQLiteCommand(query4, dbconn);
  84. reader = commandskup.ExecuteReader();
  85. while (reader.Read())
  86. Console.WriteLine("{0}. skupina - po?et ?len?: {1} -celkové kredity {2}", reader[0], reader[1], reader[2]);
  87.  
  88. //Console.WriteLine(reader.);
  89. }
  90. else if (psat == 5)
  91. {
  92. Console.WriteLine("Napiste ID studenta, kterého chcete p?idat");
  93. string idstude = Console.ReadLine();
  94. Console.WriteLine("Napiste jmeno skupiny (cislo) do které chcete studenta pridat");
  95. string pridani1 = Console.ReadLine();
  96. string query5 = string.Format("INSERT INTO Skupiny (Skupina , Studenti) VALUES('{0}', '{1}')", pridani1, idstude);
  97. SQLiteCommand commandprids = new SQLiteCommand(query5, dbconn);
  98. commandprids.ExecuteReader();
  99. Console.WriteLine("pridal jste studenta s ID " + idstude + " do skupiny " + pridani1);
  100.  
  101. //Console.WriteLine(reader.);
  102. }
  103. }
  104.  
  105.  
  106. }
  107. }

Report this snippet  

You need to login to post a comment.