方法失败从.csv读取所有行

时间:2014-10-10 23:05:09

标签: c# csv

我们有一个应用程序,用于管理我们的VOIP系统的更改。它使用通过自定义导出构建的CSV上传来控制电话的CISCO系统。我认为,在.csv文件中有一个错误的行,其中有一个" - "字符,即删除字符允许文件一直处理。读取文件的方法由StreamReader提供,StaticConfig对象是我们为每行存储记录的实体。这是读取.csv行的方法:

   while ((LineBuffer = sr.ReadLine()) != null)
                {
                    // loop through all lines found in file
                    string[] tmpString = LineBuffer.Split(Convert.ToChar(","));
                    if (tmpString[0] != "")
                    {
                        StaticConfig staticEntry = new StaticConfig();
                        int extension;
                        if (Int32.TryParse(tmpString[0].Trim(), out extension))
                        {
                            staticEntry.DirectoryNumber = extension;
                            staticEntry.Building = tmpString[9].Trim();
                            staticEntry.LongDistance = (tmpString[7].Trim() == "1");
                            staticEntry.ClassRestricted = (tmpString[6].Trim() == "1");
                            staticEntry.VoiceMail = (tmpString[8].Trim() == "1");
                            staticEntry.Location = tmpString[5].Trim();
                            staticEntry.UserFName = tmpString[1].Trim();
                            staticEntry.UserLName = tmpString[2].Trim();
                            staticEntry.UserID = tmpString[3].Trim();
                            staticEntry.EmployeeNumber = tmpString[4].Trim();
                            staticEntry.UploadDate = uploadTime;

                            //Get the locationID
                            var LocationID = (from l in voipDB.Locations
                                              where l.LocationName == staticEntry.Building
                                              select l.LocationID).FirstOrDefault();

                            if (LocationID != Guid.Empty)
                                staticEntry.LocationID = LocationID;
                            else
                            {
                                Location newLocation = new Location();
                                newLocation.LocationID = Guid.NewGuid();
                                newLocation.LocationName = staticEntry.Building;
                                newLocation.IsLocked = false;
                                newLocation.IsSubmitted = false;

                                voipDB.AddToLocations(newLocation);
                                staticEntry.LocationID = newLocation.LocationID;
                                voipDB.SaveChanges();
                            }

                            voipDB.StaticConfigs.AddObject(staticEntry);
                            processed++;
                        }
                    }
                }

对我来说没有任何意义,尽管这会在#34; - "尽管如此,我觉得我错过了一些东西。看看.Split和Convert.ToChar的MSDN写作,我看不到任何会失败的东西。谁能告诉我它为什么会失败或在哪里?关于此的任何有用的信息来源?提前谢谢了!

0 个答案:

没有答案