예제 #1
0
        public static bool LoadForeclosureFile(string sRoot, string sFile)
        {
            bool bSussess = true;

            GlobalVariables.iTotalRecords = 0;
            if (!Directory.Exists(sRoot + "\\Logs"))
            {
                Directory.CreateDirectory(sRoot + "\\Logs");
            }
            TextWriter tw = new StreamWriter(sRoot + "\\Logs" + "\\foreclosure-debug-" + sFile + ".txt");

            using (var destinationConnection = new SqlConnection(GlobalVariables.DbConnection)) {
                destinationConnection.Open();
                SqlTransaction transaction = destinationConnection.BeginTransaction();
                try {
                    tw.WriteLine("=================================================");
                    tw.WriteLine("Debug Start - " + DateTime.Now.ToString());
                    tw.WriteLine("=================================================");
                    using (var file = new StreamReader(sRoot + "\\" + sFile))
                        using (var csv = new CsvReader(file, true, '|', '"', '"', '#', ValueTrimmingOptions.All)) // true = has header row
                            using (var bcp = new SqlBulkCopy(destinationConnection, SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.KeepNulls, transaction)) {
                                csv.MissingFieldAction      = MissingFieldAction.ParseError;
                                csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
                                csv.ParseError          += csv_ParseError;
                                csv.SkipEmptyLines       = true;
                                bcp.DestinationTableName = Properties.Settings.Default.ForeclosureTable;
                                lock (GlobalVariables.locker) {
                                    bcp.NotifyAfter = GlobalVariables.iNotifyAfter;
                                    bcp.BatchSize   = GlobalVariables.iBatchSize;
                                }
                                bcp.BulkCopyTimeout = 1800;
                                bcp.SqlRowsCopied  += bcp_SqlRowsCopied;
                                var            t          = new Foreclosure();
                                Type           type       = t.GetType();
                                PropertyInfo[] properties = type.GetProperties();
                                foreach (PropertyInfo property in properties)
                                {
                                    bcp.ColumnMappings.Add(property.Name, property.Name);
                                }
                                bcp.WriteToServer(csv);
                                transaction.Commit();
                            }
                }
                catch (Exception e) {
                    tw.WriteLine(e.Message);
                    bSussess = false;
                    transaction.Rollback();
                    lock (GlobalVariables.locker) {
                        GlobalVariables.iNumberOfErrors++;
                        GlobalVariables.sForeclosureErrors += Environment.NewLine + e.Message;
                    }
                }
                finally {
                    tw.Close();
                    tw.Dispose();
                    transaction.Dispose();
                }
            }
            return(bSussess);
        }
        public ForeclosureFileDataReader(string sFileName)
        {
            _objectList = new List <Foreclosure>();
            // open the file "data.csv" which is a CSV file with headers
            using (CsvReader csv =
                       new CsvReader(new StreamReader(sFileName), true)) {
                int fieldCount = csv.FieldCount;

                string[] headers = csv.GetFieldHeaders();
                while (csv.ReadNextRecord())
                {
                    var f = new Foreclosure();
                    for (int i = 0; i < fieldCount; i++)
                    {
                        Console.WriteLine();
                    }
                }
            }

            //_objectList = o;
        }