Example #1
0
 public static void AddThing(BO_TodoThing thing)
 {
     //serial
     //DB_Acces_Serial_TodoThing.AddData(thing);
     //sql command
     DB_Acces_Client.DB_PostThing(thing);
 }
 //factoriser code de test
 public static void AddData(BO_TodoThing thing)
 {
     var dir = new DirectoryInfo(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml");
     if (dir.Exists)
     {
         if(new FileInfo(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml").Exists)
         {
             var ser = new XmlSerializer(typeof(List<BO_TodoThing>));
             var data =new List<BO_TodoThing>();
             using (var stream = new StreamReader(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"))
             {
                 data = (List<BO_TodoThing>)ser.Deserialize(stream);
             }
             data.Add(thing);
             System.IO.File.WriteAllText((@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"), string.Empty);
             using (var stream = new StreamWriter(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"))
             {
                 ser.Serialize(stream, data);
             }
         }
         else
         {
             var ser = new XmlSerializer(typeof(List<BO_TodoThing>));
             var data = new List<BO_TodoThing>();
             CreateData();
             using (var stream = new StreamReader(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"))
             {
                 data = (List<BO_TodoThing>)ser.Deserialize(stream);
             }
             data.Add(thing);
             System.IO.File.WriteAllText((@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"), string.Empty);
             using (var stream = new StreamWriter(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"))
             {
                 ser.Serialize(stream, data);
             }
         }
     }
     else
     {
         dir.Create();
         CreateData();
         var ser = new XmlSerializer(typeof(List<BO_TodoThing>));
         var data = new List<BO_TodoThing>();
         CreateData();
         using (var stream = new StreamReader(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"))
         {
             data = (List<BO_TodoThing>)ser.Deserialize(stream);
         }
         data.Add(thing);
         System.IO.File.WriteAllText((@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"), string.Empty);
         using (var stream = new StreamWriter(@"C:\Users\Karim\Documents\cours\developpement\c_sharp\3eme\projet\b.m.t\MVVM\DAL_SericalisationXML\Xml\file.xml"))
         {
             ser.Serialize(stream, data);
         }
     }
 }
        public static void DB_PostThing(BO_TodoThing thing)
        {
            
            var cmd = new SqlCommand("Client.insertToDB", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@title", thing.Title);
            try
            {
                Connect.OpenConnection(con);
                cmd.ExecuteNonQuery();
                Connect.CloseConnection(con);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        public static List<BO_TodoThing> DB_GetAllTodoThing()
        {
            
            var cmd = new SqlCommand("Client.GetAllThingToDo",con);
            cmd.CommandType = CommandType.StoredProcedure;
            var ds = exec(cmd, "thing");

            var lst = new List<BO_TodoThing>();
            foreach (DataRow r in ds.Tables["thing"].Rows)
            {
                var t = new BO_TodoThing()
                {
                    ID = Convert.ToInt32(r["Id"].ToString()),
                    Title = r["Title"].ToString(),
                    Done = Convert.ToInt32(r["Done"].ToString())
                };
                lst.Add(t);
            }
            return lst;
        }