コード例 #1
0
        public void Import(string upgradeCodeDir)
        {
            //foreach spread sheet under the dir, import it to database
            DirectoryInfo dirUpgradeCode = new DirectoryInfo(upgradeCodeDir);

            FileInfo[] files = dirUpgradeCode.GetFiles("*.csv");


            foreach (FileInfo fInfo in files)
            {
                Dictionary <string, int> mapHeaderIndex = new Dictionary <string, int>();
                string fileName = fInfo.FullName;
                LogHelper.Instance.Info("START importing UpgradeCode spreadsheet " + fileName);
                Encoding curEncoding = GetEncoding(fileName);

                try
                {
                    using (var reader = new StreamReader(fileName, curEncoding))
                    {
                        var header = reader.ReadLine().Split(',');

                        int i = 0;
                        while (i < header.Length)
                        {
                            if (!mapHeaderIndex.ContainsKey(header[i].Trim()))
                            {
                                mapHeaderIndex.Add(header[i].Trim(), i);
                                i++;
                            }
                            else
                            {
                                LogHelper.Instance.Error("Duplicate column header " + header[i] + "in " + fileName);
                                break;
                            }
                        }

                        if (i != header.Length) // break the loop due to duplicate column header name
                        {
                            continue;
                        }

                        if (!mapHeaderIndex.ContainsKey(UCColHeaderSN))
                        {
                            LogHelper.Instance.Error("Unable to find column" + UCColHeaderSN + "in " + fileName);

                            continue;
                        }
                        if (!mapHeaderIndex.ContainsKey(UCColHeaderUpgradeCode))
                        {
                            LogHelper.Instance.Error("Unable to find column" + UCColHeaderUpgradeCode + "in " + fileName);

                            continue;
                        }

                        using (var dbContext = new FcoDBEntities())
                        {
                            while (!reader.EndOfStream)
                            {
                                UpgradeResult item   = new UpgradeResult();
                                var           line   = reader.ReadLine();
                                var           values = line.Split(',');
                                if (string.IsNullOrWhiteSpace(values[mapHeaderIndex[UCColHeaderSN]]) &&
                                    string.IsNullOrWhiteSpace(values[mapHeaderIndex[UCColHeaderUpgradeCode]]))
                                {
                                    continue; //skip the empty line
                                }
                                item.Id             = Guid.NewGuid().ToString();
                                item.SystemSerialNo = values[mapHeaderIndex[UCColHeaderSN]].Trim().ToUpper();
                                item.UpgradeCode    = values[mapHeaderIndex[UCColHeaderUpgradeCode]].Trim().ToUpper();
                                if (mapHeaderIndex.ContainsKey(UCColHeaderModelNumber)) //TC upgrade code
                                {
                                    item.ModelNumber = values[mapHeaderIndex[UCColHeaderModelNumber]].Trim().ToUpper();
                                }

                                dbContext.UpgradeResults.Add(item);
                            }

                            dbContext.SaveChanges();
                        } //End dbcontext
                    }     //End stream reader

                    FCOPath fcoPath         = new FCOPath(m_dt);
                    string  archiveFilePath = fcoPath.GetArchiveFilePath();
                    ArchiveTo(fileName, archiveFilePath);
                    LogHelper.Instance.Info(string.Format("SUCCESSFULLY imported {0} to database.", fileName));
                }
                catch (Exception e1)
                {
                    LogHelper.Instance.Error(string.Format("FAIL to import upgradecode file {0} due to {1}.", fileName, e1.Message));
                    continue;
                }
            }
        }