Exemplo n.º 1
0
        public override void ImportFromDataBase(DateTime date, string Name, string LastName,
                                                string SurName, string City, string Country)
        {
            string path = @"C:\Users\User\source\repos\IBA_Tasks\Task-1\DataInteraction\ExcelFile.xlsx";

            Excel.Application excelapp  = new Excel.Application();
            Excel.Workbook    workbook  = excelapp.Workbooks.Add();
            Excel.Worksheet   worksheet = (Excel.Worksheet)workbook.ActiveSheet;

            int CurrentRow = 0;

            using (ConnectionDataBase db = new ConnectionDataBase())
            {
                var Humen = db.HumenDataSearch(date, Name, LastName, SurName, City, Country);

                foreach (Human human in Humen)
                {
                    CurrentRow++;

                    worksheet.Rows[CurrentRow, 1] = human.Date;
                    worksheet.Rows[CurrentRow, 2] = human.FirstName;
                    worksheet.Rows[CurrentRow, 3] = human.LastName;
                    worksheet.Rows[CurrentRow, 4] = human.SurName;
                    worksheet.Rows[CurrentRow, 5] = human.City;
                    worksheet.Rows[CurrentRow, 6] = human.Country;
                }
            }

            excelapp.AlertBeforeOverwriting = false;
            workbook.SaveAs(path);
            excelapp.Quit();
        }
Exemplo n.º 2
0
        public override void ExportToDataBase()
        {
            //Открытие соединения с БД
            using (ConnectionDataBase db = new ConnectionDataBase())
            {
                //Открытие потока для записи
                using (TextFieldParser tfp = new TextFieldParser(PathCsvFile))
                {
                    //Задание разделителя по условию ";"
                    tfp.TextFieldType = FieldType.Delimited;
                    tfp.SetDelimiters(";");

                    //Считывание будет продолжаться пока не дойдет до конца файла
                    while (!tfp.EndOfData)
                    {
                        //Список полей одной строки
                        string[] fields = tfp.ReadFields();

                        //Добавление новой записи в БД
                        db.Humen.Add(new Human()
                        {
                            Date      = DateTime.Parse(fields[0]),
                            FirstName = fields[1],
                            LastName  = fields[2],
                            SurName   = fields[3],
                            City      = fields[4],
                            Country   = fields[5]
                        });
                    }
                    //Сохрание изменений
                    db.SaveChanges();
                }
            }
        }
Exemplo n.º 3
0
        public override void ImportFromDataBase(DateTime date, string Name, string LastName,
                                                string SurName, string City, string Country)
        {
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(@"C:\Users\User\source\repos\IBA_Tasks\Task-1\DataInteraction\XML.xml");

            //Открытие соединения с БД
            using (ConnectionDataBase db = new ConnectionDataBase())
            {
                var Humen = db.HumenDataSearch(date, Name, LastName, SurName, City, Country);

                foreach (Human human in Humen)
                {
                    XmlElement xRoot = xDoc.DocumentElement;
                    // Создаем новую запись.
                    XmlElement RecordElem = xDoc.CreateElement("Record");
                    // Создаем атрибут ID.
                    XmlAttribute idAttr = xDoc.CreateAttribute("ID");
                    // Создаем элементы Date, FirstName, LastName, SurName, City, Country.
                    XmlElement DateElem      = xDoc.CreateElement("Date");
                    XmlElement FirstNameElem = xDoc.CreateElement("FirstName");
                    XmlElement LastNameElem  = xDoc.CreateElement("LastName");
                    XmlElement SurNameElem   = xDoc.CreateElement("SurName");
                    XmlElement CityElem      = xDoc.CreateElement("City");
                    XmlElement CountryElem   = xDoc.CreateElement("Country");

                    // Создаем текстовые значения для элементов и атрибута.
                    XmlText idText        = xDoc.CreateTextNode(human.HumanID.ToString());
                    XmlText DateText      = xDoc.CreateTextNode(human.Date.ToString("d"));
                    XmlText FirstNameText = xDoc.CreateTextNode(human.FirstName);
                    XmlText LastNameText  = xDoc.CreateTextNode(human.LastName);
                    XmlText SurNameText   = xDoc.CreateTextNode(human.SurName);
                    XmlText CityText      = xDoc.CreateTextNode(human.City);
                    XmlText CountryText   = xDoc.CreateTextNode(human.Country);

                    // Добавляем узлы.
                    idAttr.AppendChild(idText);
                    DateElem.AppendChild(DateText);
                    FirstNameElem.AppendChild(FirstNameText);
                    LastNameElem.AppendChild(LastNameText);
                    SurNameElem.AppendChild(SurNameText);
                    CityElem.AppendChild(CityText);
                    CountryElem.AppendChild(CountryText);

                    RecordElem.Attributes.Append(idAttr);
                    RecordElem.AppendChild(DateElem);
                    RecordElem.AppendChild(FirstNameElem);
                    RecordElem.AppendChild(LastNameElem);
                    RecordElem.AppendChild(SurNameElem);
                    RecordElem.AppendChild(CityElem);
                    RecordElem.AppendChild(CountryElem);

                    xRoot.AppendChild(RecordElem);
                }
            }

            xDoc.Save(@"C:\Users\User\source\repos\IBA_Tasks\Task-1\DataInteraction\XML.xml");
        }