Advertisement
Guest User

Fonctions utilisation base de données

a guest
May 27th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. using MongoDB.Driver;
  2. using System;
  3. using System.Threading.Tasks;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Linq;
  7. using MongoDB.Bson;
  8. using MongoDB.Driver.Core;
  9.  
  10.  
  11.  
  12. namespace testdesfonctions
  13. {
  14. internal class Student
  15. {
  16. public ObjectId _id { get; set; }
  17. public string FirstName { get; set; }
  18. public string LastName { get; set; }
  19. public string Class { get; set; }
  20. public int Age { get; set; }
  21. public IEnumerable<string> Subjects { get; set; }
  22. }
  23. public class MongoCRUD
  24. {
  25. private IMongoDatabase db;
  26.  
  27. public MongoCRUD(string database)
  28. { /*connecte à database ou cree la base du nom de database */
  29. var client = new MongoClient("mongodb://localhost");
  30. db = client.GetDatabase(database);
  31.  
  32. }
  33.  
  34. public void InsertRecord<T>(string table, T record)
  35. { /*ajouter un element record (sous forme ) dans une collection table, si table inexistant creation automatique */
  36. var collection = db.GetCollection<T>(table);
  37. collection.InsertOne(record);
  38. }
  39.  
  40. public List<T> LoadRecords<T>(string table)
  41. { /*lire une collection entière*/
  42. var collection = db.GetCollection<T>(table);
  43. return collection.Find(new BsonDocument()).ToList();
  44. }
  45.  
  46. public T LoadRecordById<T>(string table, ObjectId id)
  47. { /*chercher l'element de la collection table grace a son id */
  48. var collection = db.GetCollection<T>(table);
  49. var filter = Builders<T>.Filter.Eq("_id", id);
  50. return collection.Find(filter).First();
  51. }
  52.  
  53. public void UpsetRecord<T>(string table, ObjectId id, T record)
  54. { /*changer l'element id dans une collection par l'element record. Il est possible de remplacer une seule propriete de l'element */
  55. var collection = db.GetCollection<T>(table);
  56. var result = collection.ReplaceOne(
  57. new BsonDocument("_id", id),
  58. record,
  59. new UpdateOptions { IsUpsert = true });
  60. }
  61.  
  62. public void DeleteRecord<T>(string table, ObjectId id)
  63. { /*supprimer un element identifie par son id */
  64. var collection = db.GetCollection<T>(table);
  65. var filter = Builders<T>.Filter.Eq("_id", id);
  66. collection.DeleteOne(filter);
  67. }
  68.  
  69. public List<T> LoadRecordByParameterString<T>(string table, string parameter, string parameterValue)
  70. { /*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode string) */
  71. var collection = db.GetCollection<T>(table);
  72. var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
  73. return collection.Find(filter).ToList();
  74. }
  75.  
  76. public List<T> LoadRecordByParameterInt<T>(string table, string parameter, int parameterValue)
  77. {/*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode int) */
  78. var collection = db.GetCollection<T>(table);
  79. var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
  80. return collection.Find(filter).ToList();
  81. }
  82.  
  83. public List<T> LoadRecordByParameterFloat<T>(string table, string parameter, float parameterValue)
  84. {/*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode float) */
  85. var collection = db.GetCollection<T>(table);
  86. var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
  87. return collection.Find(filter).ToList();
  88. }
  89.  
  90. public List<T> LoadRecordByParameterBool<T>(string table, string parameter, bool parameterValue)
  91. {/*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode bool) */
  92. var collection = db.GetCollection<T>(table);
  93. var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
  94. return collection.Find(filter).ToList();
  95. }
  96.  
  97.  
  98. }
  99. class Program
  100. {
  101. static void Main(string[] args)
  102. {
  103. MainAsync().Wait();
  104.  
  105.  
  106.  
  107.  
  108. Console.WriteLine("Press enter to exit");
  109. Console.ReadLine();
  110. }
  111.  
  112.  
  113. static async Task MainAsync()
  114. {
  115. /*connection a la base school */
  116. MongoCRUD db = new MongoCRUD("schoool");
  117.  
  118. /*ajout d'un nouvel eleve */
  119. /*var studentAlexandre = new Student
  120. {
  121. _id = new ObjectId("5ce7fa420a7ff85b5154c2e5"),
  122. FirstName = "Alexandre",
  123. LastName = "Dupond",
  124. Subjects = new List<string> { "Elektronik", "Mathematik", "Phisik" },
  125. Class = "CSI 2",
  126. Age = 5
  127. };
  128. db.InsertRecord("students", studentAlexandre);*/
  129.  
  130.  
  131. /*changement d'informations Gregor (id 5ce7fa420a7ff85b5154c2e5) va changer de classe */
  132. ObjectId myId = new ObjectId("5ce7fa420a7ff85b5154c2e5");
  133. var oneRec = db.LoadRecordById<Student>("students", myId);
  134. oneRec.Class = "CSI 3";
  135. db.UpsetRecord<Student>("students", myId, oneRec);
  136.  
  137.  
  138. /*supprimons Alexandre (id 5ce7fa420a7ff85b5154c2e5)*/
  139. /*ObjectId myIdBis = new ObjectId("5ce7fa420a7ff85b5154c2e5");
  140. db.DeleteRecord<Student>("students", myIdBis);*/
  141.  
  142. /*affichons la liste des etudiants en classe "JSS 3"*/
  143. var recs = db.LoadRecordByParameterString<Student>("students", "Class", "JSS 3");
  144. foreach (var rec in recs) {
  145. Console.WriteLine($"{rec._id}:{rec.FirstName}:{rec.LastName}:{rec.Class}:{rec.Age}"); }
  146.  
  147. Console.WriteLine("\n");
  148.  
  149. /*affichons la liste des etudiants qui ont "23" ans*/
  150. var rics = db.LoadRecordByParameterInt<Student>("students", "Age", 23);
  151. foreach (var ric in rics)
  152. {
  153. Console.WriteLine($"{ric._id}:{ric.FirstName}:{ric.LastName}:{ric.Class}:{ric.Age}");
  154. }
  155.  
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement