示例#1
0
        private void ImportSparesFromCSV()
        {
            ConsoleManager.Show();
            Console.WriteLine(DateTime.Now.ToShortTimeString() + " - started...");
            string FilePath = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = ".csv"; // Default file extension

            //    dlg.Filter = "*.csv"; // Filter files by extension
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                DataAccess da = new DataAccess();

                // Open document
                FilePath = dlg.FileName;
                int counter = 0;
                int GlobalCnt = 0;
                using (CsvReader csvData = new CsvReader(FilePath))
                {
                    csvData.Settings.Delimiter = ';';
                    while (csvData.ReadRecord())
                    {
                        try
                        {
                            counter++;
                            GlobalCnt++;
                            string[] RawRecord = csvData.RawRecord.Split(';');
                            string GrandParentGroupName = RawRecord[0];
                            string ParentGroupName = RawRecord[1];
                            string BrandName = RawRecord[2];
                            string SpareCode = RawRecord[3];
                            string SpareName = RawRecord[4];
                            string CarModels = RawRecord[5];

                            if (!da.SpareExist(SpareCode, SpareName))
                            {
                                Console.WriteLine(GlobalCnt.ToString() + ": " + SpareName + " - " + SpareCode + " - creating");
                                da.SpareCreate(GrandParentGroupName, ParentGroupName, BrandName, SpareCode, SpareName, CarModels);
                            }
                            else
                            {
                                Console.WriteLine(GlobalCnt.ToString() + ": " + SpareName + " - " + SpareCode + " already exist");
                            }
                            if (counter > 1000)
                            {
                                counter = 0;
                                da = new DataAccess();
                                Console.WriteLine("Data access object refresh");
                            }
                        }
                        catch (Exception e1)
                        {
                            Console.WriteLine(GlobalCnt.ToString() + ": Exception failed: " + e1.Message);
                        }
                    }
                } // dispose of parser
            }
            Console.WriteLine(DateTime.Now.ToShortTimeString() + " - finished...");
            MessageBox.Show("Import finished!");
            Console.ReadLine();
            ConsoleManager.Hide();
            Application.Current.Shutdown();
        }