Beispiel #1
0
        private void BtnCsvChoose_Click(object sender, RoutedEventArgs e)
        {
            var             openFileDlg = new OpenFileDialog();
            Nullable <bool> result      = openFileDlg.ShowDialog();

            allRecordsCount.Content = "Total records count: " + 0;

            if (result == true)
            {
                filePathLabel.Text = openFileDlg.FileName;
                var dynamicCsvInfo = CsvReaderCustom.Read <DynamicCsv>(openFileDlg.FileName, false);

                allRecordsCount.Content = "Total records count: " + dynamicCsvInfo.AllRecordsCount;

                var sampleDataList = new List <DynamicCsv>();

                if (dynamicCsvInfo.IsHeadless)
                {
                    dynamicCsvInfo          = CsvReaderCustom.Read <DynamicCsv>(openFileDlg.FileName, true);
                    allRecordsCount.Content = "Total records count: " + dynamicCsvInfo.AllRecordsCount;
                }
                if (dynamicCsvInfo.Records.Count >= 30)
                {
                    sampleDataList = dynamicCsvInfo.Records.Take(30).ToList();
                }
                else
                {
                    sampleDataList = dynamicCsvInfo.Records.ToList();
                }

                if (sampleDataList.Count == 0)
                {
                    return;
                }

                RecordsToInsert = new List <DynamicCsv>();
                RecordsToInsert.AddRange(dynamicCsvInfo.Records);
                btnCsvImport.IsEnabled = true;

                var dataListObservable = new ObservableCollection <DynamicCsv>(sampleDataList);

                this.csvDataGrid.ItemsSource = dataListObservable;

                SetEqualColumnWidth(csvDataGrid);
            }
        }
        /*
         * static void Main(string[] args)
         * {
         * PromptUser();
         *
         * }
         */

        private static void PromptUser()
        {
            Console.WriteLine("Paste/Type File Path:");
            var filePath = Console.ReadLine();

            Console.WriteLine("Number of columns in CSV file :");
            var numberOfColumns = int.Parse(Console.ReadLine());

            Console.WriteLine("Does CSV file have header (column names) ?  Y/N :");
            var hasHeader = Console.ReadLine() == "Y" ? true : false;

            Console.WriteLine("Test if it works or copy to database ?  TEST/COPY :");
            var testOnly = Console.ReadLine() == "TEST" ? true : false;

            Console.WriteLine("Max records at once (max 1000) ?  TEST/COPY :");
            var inputedRecords   = int.Parse(Console.ReadLine());
            var maxRecordsAtOnce = inputedRecords > 1000 ? inputedRecords : 1000;

            var csvParams = new CsvSetupParams()
            {
                FilePath        = filePath,
                NumberOfColumns = numberOfColumns,
                HasHeader       = hasHeader,
                TestOnly        = testOnly
            };

            var csvTouple = CsvReaderCustom.ReadByType(csvParams);

            if (testOnly)
            {
                CsvWriterCustom.WriteToConsole(csvTouple.csvList);
            }
            else
            {
                CsvWriterCustom.WriteToDatabase(csvTouple.csvList, maxRecordsAtOnce);
            }
        }