Exemple #1
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFile.Text))
            {
                MessageBox.Show("File cannot be empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                rtbImportData.Text = $"Starting...{Environment.NewLine}{Environment.NewLine}";
                using (var reader = new StreamReader(txtFile.Text))
                {
                    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                    {
                        var records = csv.GetRecords <CSVImportHeader>();
                        foreach (var record in records)
                        {
                            var recordTemp       = record;
                            var strSeparators    = new string[] { "\n" };
                            var listAuthorNames  = record.AuthorName.Split(strSeparators, StringSplitOptions.None);
                            var listAuthorEmails = record.AuthorEmail.Split(strSeparators, StringSplitOptions.None);
                            var author           = new Dictionary <string, string>();
                            for (int i = 0; i < listAuthorNames.Length; i++)
                            {
                                // Write process
                                rtbImportData.Text += $"Processing... ID: {recordTemp.ID} | Title: {recordTemp.Title}{Environment.NewLine}";

                                recordTemp.AuthorName  = listAuthorNames[i];
                                recordTemp.AuthorEmail = listAuthorEmails[i];
                                var storedImportCSV = BuildingStoredProcHelper.ImportDataFromCSV(recordTemp);
                                var result          = HandlingDataHelper.ImportData(storedImportCSV);

                                // Write result
                                rtbImportData.Text += $"{result}{Environment.NewLine}{Environment.NewLine}";
                                rtbImportData.Text += $"=================================================={Environment.NewLine}{Environment.NewLine}";
                            }
                        }
                    }
                }
                rtbImportData.Text += $"Finished!";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error when importing. Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }