Beispiel #1
0
        public override ImportRowSource CreateRowSource(IProgressObserver progress)
        {
            if (_options == null)
            {
                throw new Exception("Null or incorrect options type received!");
            }

            ImportRowSource rowsource = null;

            using (var parser = new GenericParserAdapter(_options.Filename)) {
                parser.ColumnDelimiter   = _options.Delimiter[0];
                parser.FirstRowHasHeader = _options.FirstRowContainsNames;
                parser.TextQualifier     = '\"';
                parser.FirstRowSetsExpectedColumnCount = true;

                var service     = new ImportStagingService();
                var columnNames = new List <String>();

                int rowCount = 0;
                service.BeginTransaction();
                var values = new List <string>();
                while (parser.Read())
                {
                    if (rowCount == 0)
                    {
                        for (int i = 0; i < parser.ColumnCount; ++i)
                        {
                            if (_options.FirstRowContainsNames)
                            {
                                columnNames.Add(parser.GetColumnName(i));
                            }
                            else
                            {
                                columnNames.Add("Column" + i);
                            }
                        }
                        service.CreateImportTable(columnNames);
                    }

                    values.Clear();
                    for (int i = 0; i < parser.ColumnCount; ++i)
                    {
                        values.Add(parser[i]);
                    }

                    service.InsertImportRow(values);

                    rowCount++;
                }

                service.CommitTransaction();

                rowsource = new ImportRowSource(service, rowCount);
            }

            return(rowsource);
        }
        private bool Validate()
        {
            if (string.IsNullOrWhiteSpace(txtFilename.Text))
            {
                ErrorMessage.Show("You must specify an input file!");
                return(false);
            }

            if (!File.Exists(txtFilename.Text))
            {
                ErrorMessage.Show("File does not exist!");
                return(false);
            }

            // Extract Column Headers...
            ColumnNames = new List <String>();

            GenericParser parser = null;

            try {
                using (parser = new GenericParserAdapter(Filename)) {
                    parser.ColumnDelimiter   = Delimiter.ToCharArray()[0];
                    parser.FirstRowHasHeader = IsFirstRowContainNames;
                    parser.MaxRows           = 2;
                    parser.TextQualifier     = _textQualifier;

                    if (parser.Read())
                    {
                        for (int i = 0; i < parser.ColumnCount; ++i)
                        {
                            if (IsFirstRowContainNames)
                            {
                                ColumnNames.Add(parser.GetColumnName(i));
                            }
                            else
                            {
                                ColumnNames.Add("Column" + i);
                            }
                        }
                    }
                    else
                    {
                        ErrorMessage.Show("Failed to extract column names from data source!");
                        return(false);
                    }
                }
            } finally {
                if (parser != null)
                {
                    System.GC.SuppressFinalize(parser);
                }
            }

            return(true);
        }
        /// <summary>
        /// Read Data from selected csv file
        /// </summary>
        public void Parse()
        {
            if (_data != null)
            {
                return;
            }

            try
            {
                _data = new DataTable();

                var csv = FromK2File(_settings.File);

                using (var textReader = new StringReader(csv))
                    using (var parser = new GenericParserAdapter(textReader))
                    {
                        parser.ColumnDelimiter   = _settings.ColumnDelimiter;
                        parser.TextQualifier     = _settings.TextQualifier;
                        parser.FirstRowHasHeader = true;
                        parser.StripControlChars = true;

                        var isFirstRow = true;

                        while (parser.Read())
                        {
                            if (isFirstRow)
                            {
                                for (var i = 0; i < parser.ColumnCount; i++)
                                {
                                    var columnName = parser.GetColumnName(i).FormatColumnName(_settings.HeaderRowSpaces);
                                    _data.Columns.Add(columnName);
                                }

                                isFirstRow = false;
                            }

                            var newRow = _data.NewRow();

                            for (var i = 0; i < parser.ColumnCount; i++)
                            {
                                newRow[i] = parser[i];
                            }

                            _data.Rows.Add(newRow);
                        }
                    }

                if (_data.Columns.Count == 0)
                {
                    Status  = ImportStatus.NoColumnsFound;
                    Message = "No columns found.";
                }
                else if (_data.Rows.Count == 0)
                {
                    Status  = ImportStatus.NoRowsFound;
                    Message = "No rows found.";
                }
                else
                {
                    Status  = ImportStatus.Complete;
                    Message =
                        $"{_data.Columns.Count} columns found: " +
                        $"'{string.Join("', '", _data.Columns.Cast<DataColumn>().Select(c => c.ColumnName))}'. " +
                        $"{_data.Rows.Count} rows parsed for import. ";
                }
            }
            catch (Exception ex)
            {
                Status  = ImportStatus.Error;
                Message = ex.Message;
            }
        }
Beispiel #4
0
        private void DoImport()
        {
            if (String.IsNullOrEmpty(txtFilename.Text))
            {
                ErrorMessage.Show("You must select a file before proceeding!");
                return;
            }

            int rowCount = 0;

            var service = new SupportService(User);

            using (var parser = new GenericParserAdapter(txtFilename.Text)) {
                parser.ColumnDelimiter   = ',';
                parser.FirstRowHasHeader = chkFirstRowHeaders.IsChecked.GetValueOrDefault(false);
                parser.TextQualifier     = '\"';
                parser.FirstRowSetsExpectedColumnCount = true;

                var columnNames = new List <String>();
                var values      = new List <string>();
                while (parser.Read())
                {
                    if (rowCount == 0)
                    {
                        for (int i = 0; i < parser.ColumnCount; ++i)
                        {
                            if (parser.FirstRowHasHeader)
                            {
                                columnNames.Add(parser.GetColumnName(i));
                            }
                            else
                            {
                                columnNames.Add("Column" + i);
                            }
                        }
                    }
                    values.Clear();
                    for (int i = 0; i < parser.ColumnCount; ++i)
                    {
                        values.Add(parser[i]);
                    }

                    String strFullPath = null;
                    if (values.Count == 0)
                    {
                        strFullPath = values[0];
                    }
                    else
                    {
                        strFullPath = values.Join("\\");
                    }

                    if (!String.IsNullOrWhiteSpace(strFullPath))
                    {
                        service.GetDistributionIdFromPath(strFullPath);
                        lblProgress.InvokeIfRequired(() => {
                            lblProgress.Content = strFullPath;
                            lblProgress.UpdateLayout();
                        });
                        rowCount++;
                    }
                }
            }

            lblProgress.InvokeIfRequired(() => {
                lblProgress.Content = String.Format("{0} rows processed.", rowCount);
            });
            btnCancel.Content = "_Close";
        }