Ejemplo n.º 1
0
        /// <summary>
        /// When the list is converted to CSV.
        /// </summary>
        /// <param name="options">
        /// The options for serializations.
        /// </param>
        public void when_the_list_is_persisted_to_csv(CsvWriterOptions options = null)
        {
            Console.WriteLine("When the list is converted to CSV.");
            Stream stream;

            if (this.items != null)
            {
                stream = CsvWriter.ToCsv(this.items, options);
            }
            else if (this.dateTimeItems != null)
            {
                stream = CsvWriter.ToCsv(this.dateTimeItems, options);
            }
            else if (this.doubleTestItems != null)
            {
                stream = CsvWriter.ToCsv(this.doubleTestItems, options);
            }
            else if (this.dynamicTestItems != null)
            {
                stream = CsvWriter.ToCsv(this.dynamicTestItems, options);
            }
            else if (this.indexerTestItems != null)
            {
                stream = CsvWriter.ToCsv(this.indexerTestItems, options);
            }
            else
            {
                throw new InvalidOperationException("No items to serialized are specified.");
            }

            using (var reader = new StreamReader(stream))
            {
                this.csv = reader.ReadToEnd();
            }
        }
Ejemplo n.º 2
0
        public void when_the_list_is_persisted_to_csv_file(CsvWriterOptions options = null)
        {
            Console.WriteLine("When the list is converted to CSV.");
            string csvFilePath = Path.GetTempFileName();

            if (this.items != null)
            {
                CsvWriter.ToCsvFile(this.items, csvFilePath, options);
            }
            else if (this.dateTimeItems != null)
            {
                CsvWriter.ToCsvFile(this.dateTimeItems, csvFilePath, options);
            }
            else if (this.doubleTestItems != null)
            {
                CsvWriter.ToCsvFile(this.doubleTestItems, csvFilePath, options);
            }
            else if (this.dynamicTestItems != null)
            {
                CsvWriter.ToCsvFile(this.dynamicTestItems, csvFilePath, options);
            }
            else if (this.indexerTestItems != null)
            {
                CsvWriter.ToCsv(this.indexerTestItems, options);
            }
            else
            {
                throw new InvalidOperationException("No items to serialized are specified.");
            }

            this.csv = File.ReadAllText(csvFilePath);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static CsvWriterOptions SetCsvWriterConfig(Options options)
        {
            CsvHelper.CsvWriterOptions csvWriterOptions = new CsvWriterOptions();
            try
            {
                switch (options.Delimiter)
                {
                case "'\\t'":
                    csvWriterOptions.Delimiter = '\t';
                    break;

                case "\\t":
                    csvWriterOptions.Delimiter = '\t';
                    break;

                default:
                    csvWriterOptions.Delimiter = char.Parse(options.Delimiter);
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to set delimiter. Defaulting to comma \",\": " + ex.Message);
                csvWriterOptions.Delimiter = ',';
            }

            return(csvWriterOptions);
        }
Ejemplo n.º 4
0
        internal static int Export(bool gui, string dsn, string userName, string password, string sql, string outputPath, char delimiter, DoWorkEventArgs e)
        {
            try
            {
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException("Unable to delete file: " + outputPath, ex);
                return(1);
            }

            logger.Info("DSN: " + dsn);
            string conString = string.Format("DSN={0};Uid={1};Pwd={2};", dsn, userName, password);

            logger.Debug("Connection string: " + conString);
            using (OdbcConnection con = new OdbcConnection(conString))
            {
                try
                {
                    logger.Debug("Opening database connection");
                    con.Open();
                    OdbcCommand com = new OdbcCommand(sql, con);

                    logger.Debug("Executing command");

                    if (worker != null)
                    {
                        worker.ReportProgress(1, "Executing sql statement");
                    }

                    logger.Info("Running SQL statement:\n" + sql);
                    using (IDataReader r = com.ExecuteReader())
                    {
                        logger.Debug("Opening text writer");

                        if (gui)
                        {
                            worker.ReportProgress(1, "Outputting records");
                        }
                        using (TextWriter tw = new StreamWriter(outputPath, false, System.Text.Encoding.UTF8))
                        {
                            CsvWriterOptions options = new CsvWriterOptions();
                            options.HasHeaderRecord = true;
                            options.Delimiter       = delimiter;

                            CsvWriter writer = new CsvWriter(tw, options);

                            logger.Debug("Writing headers");
                            // Write the header
                            for (int i = 0; i < r.FieldCount; i++)
                            {
                                writer.WriteField(r.GetName(i));
                            }

                            writer.NextRecord();

                            int count     = 0;
                            int lastCount = 0;
                            int gap       = 1000;
                            if (!gui)
                            {
                                gap = 10000;
                            }

                            Random rnd = new Random();

                            logger.Debug("Writing data...");
                            // Write the values
                            while (r.Read())
                            {
                                if (gui && worker.CancellationPending)
                                {
                                    e.Cancel = true;
                                    break;
                                }

                                count++;


                                //logger.Debug("Writing record: " + count.ToString("N0"));

                                // Output all fields
                                for (int i = 0; i < r.FieldCount; i++)
                                {
                                    if (r.IsDBNull(i))
                                    {
                                        writer.WriteField("");
                                    }
                                    else if (r.GetFieldType(i) == typeof(DateTime))
                                    {
                                        writer.WriteField(r.GetDateTime(i).ToString("yyyy-MM-dd HH:mm:ss"));
                                    }
                                    else
                                    {
                                        writer.WriteField(r[i].ToString().Trim());
                                    }
                                }

                                writer.NextRecord();

                                if (count >= lastCount + (gap + gap * rnd.NextDouble()))
                                {
                                    if (gui)
                                    {
                                        worker.ReportProgress(count);
                                    }
                                    else
                                    {
                                        Console.Write(count.ToString("N0") + "   ");
                                    }

                                    lastCount = count;
                                }
                            }

                            tw.Flush();
                        }
                    }

                    if (gui)
                    {
                        worker.ReportProgress(0);
                    }


                    logger.Debug("Process complete");
                }
                catch (Exception ex)
                {
                    Program.logger.ErrorException("Export", ex);
                    if (gui)
                    {
                        throw ex;
                    }
                    return(1);
                }
            }

            return(0);
        }