예제 #1
0
 public static void UpdatePublisher()
 {
     using (LibEntities library = new LibEntities())
     {
         Console.Write("Read the publisher's ID in order to change his name: ");;
         int PID    = Convert.ToInt32(Console.ReadLine());
         var result = library.Publishers.SingleOrDefault(p => p.PublisherId == PID);
         if (result != null)
         {
             Console.Write("Read the new publisher's name: ");
             result.Name = Console.ReadLine();
             library.SaveChanges();
         }
         else
         {
             Console.WriteLine("The publisher could not be found");
         }
     }
 }
예제 #2
0
        public static void SavePublisher()
        {
            using (LibEntities library = new LibEntities())
            {
                // Create Publisher
                Publisher p = new Publisher();
                Console.Write("Read the publisher's name: ");
                p.Name = Console.ReadLine();

                // Add Publisher to DB
                library.Publishers.Add(p);
                library.SaveChanges();
                Console.WriteLine("\n\t Publisher added to the DB.\n");

                // Write to .XML file
                Console.WriteLine("\t XML");
                using (FileStream stream = new FileStream("Publishers.xml", FileMode.Create))
                {
                    XmlSerializer XML = new XmlSerializer(typeof(Publisher));
                    XML.Serialize(stream, p);
                }
                Console.WriteLine("\n\t Publisher added to 'Publishers.xml'\n");

                // Write to .JSON file
                Console.WriteLine("\t JSON");
                string publisherResult = JsonConvert.SerializeObject(p);
                using (StreamWriter file = new StreamWriter(@"Publishers.json", true))
                {
                    file.WriteLine(publisherResult);
                    file.Flush();
                    file.Close();
                }
                Console.WriteLine("\n\t Publisher added to 'Publishers.json'\n");
                Console.WriteLine($"\n\t JSON-Test ==> {publisherResult}\n"); // test
            }
        }