예제 #1
0
        //
        // Event handlers
        //

        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            TextWriter writer = null;

            try
            {
                writer = new StreamWriter(OutputFileTextBox.Text);
                int count = 0;
                //List<string> fields = new List<string>();
                SimpleCsvWriter csvWriter = new SimpleCsvWriter();
                // newTable used only to get at column info
                LogAddressesGoodNewTable newTable = new LogAddressesGoodNewTable();
                using (var reader = LogAddressesGood.GetAllDataReader(0))
                {
                    while (reader.Read())
                    {
                        //fields.Clear();
                        //fields.Add("NULL"); // Id (will auto-increment)
                        //AddField(fields, reader.DateStamp.ToString("yyyy-MM-dd HH:mm:ss")); // DateStamp
                        //AddField(fields, ""); // Email
                        //AddField(fields, ""); // RawAddress
                        //AddField(fields, ""); // SelectedStateCode
                        csvWriter.AddNull();                                                  // Id (will auto-increment)
                        csvWriter.AddField(reader.DateStamp.ToString("yyyy-MM-dd HH:mm:ss")); // DateStamp
                        csvWriter.AddField("");                                               // Email
                        csvWriter.AddField("");                                               // RawAddress
                        csvWriter.AddField("");                                               // SelectedStateCode
                        // concatenate the address fields
                        string address  = reader.Address1.Trim();
                        string address2 = reader.Address2.Trim();
                        if (address.Length == 0)
                        {
                            address = address2;
                        }
                        else if (address2.Length > 0)
                        {
                            address = address + ", " + address2;
                        }
                        //AddField(fields, newTable.ParsedAddressColumn.Truncate(address));
                        //AddField(fields, newTable.ParsedCityColumn.Truncate(reader.City));
                        //AddField(fields, newTable.ParsedStateCodeColumn.Truncate(reader.StateCode));
                        //AddField(fields, newTable.ParsedZip5Column.Truncate(reader.Zip5));
                        //AddField(fields, newTable.ParsedZip4Column.Truncate(reader.Zip4));
                        csvWriter.AddField(newTable.ParsedAddressColumn.Truncate(address));
                        csvWriter.AddField(newTable.ParsedCityColumn.Truncate(reader.City));
                        csvWriter.AddField(newTable.ParsedStateCodeColumn.Truncate(reader.StateCode));
                        csvWriter.AddField(newTable.ParsedZip5Column.Truncate(reader.Zip5));
                        csvWriter.AddField(newTable.ParsedZip4Column.Truncate(reader.Zip4));
                        //writer.WriteLine(string.Join(",", fields));
                        csvWriter.Write(writer);
                        count++;
                        if ((count % 10000) == 0)
                        {
                            AppendStatusText("{0} rows written", count);
                        }
                    }
                    AppendStatusText("{0} rows written", count);
                    AppendStatusText("Finished", count);
                }
            }
            catch (VoteException ex)
            {
                AppendStatusText(ex.Message);
                AppendStatusText("Terminated.");
            }
            catch (Exception ex)
            {
                AppendStatusText(ex.ToString());
                AppendStatusText("Terminated.");
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }