예제 #1
0
        static void PrintDB()
        {
            Console.WriteLine("\nDatabase ..");

            using (var db = new iDeskDbContext())
            {
                foreach (var incident in db.Incidents)
                {
                    Console.WriteLine(incident.IncidentID, incident.Summary);
                }
            }
        }
예제 #2
0
        static void exportReportToDB()
        {
            Console.WriteLine("Exporting Report Data to DB ...");

            //Read Excel
            FileInfo excelReport = new FileInfo("./files/reports/Book.xlsx");

            using (ExcelPackage excelPackage = new ExcelPackage(excelReport))
            {
                ExcelWorksheet firstSheet = excelPackage.Workbook.Worksheets[0];

                Console.WriteLine("\nExcel ...");
                Console.WriteLine("Total Rows - " + firstSheet.Dimension.Rows.ToString());

                using (var db = new iDeskDbContext())
                {
                    for (int i = 2; i <= firstSheet.Dimension.Rows; i++)
                    {
                        Console.WriteLine(firstSheet.Cells[i, 1].Text);

                        Incident incident = new Incident();

                        Type           type       = typeof(Incident);
                        PropertyInfo[] properties = type.GetProperties();
                        foreach (PropertyInfo property in properties)
                        {
                            property.SetValue(incident, firstSheet.Cells[i, property.GetCustomAttribute <ExcelColPos>().Colpos].Text);
                        }

                        db.Incidents.Add(incident);

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Microsoft.EntityFrameworkCore.DbUpdateException e)
                        {
                            db.Incidents.Update(incident);
                            db.SaveChanges();
                        }
                    }

                    ControlParam control = new ControlParam();
                    control.Key   = "Last Executed On";
                    control.Value = System.DateTime.Now.ToString();
                    db.ControlParams.Update(control);
                    db.SaveChanges();
                }
            }

            //PrintDB();
        }