2016-07-25 147 views
3

我有一个页面,大约10分钟的时间间隔将csv文件上传到文件夹(通过http链接接收)。这个csv文件必须上传到sql。我已经设法得到csv文件并将它们保存在文件夹中,但是我得到的问题是,当我尝试读取数据时,它显示文件是空的(但它不是)...它不会抛出任何错误,但是当我使用调试运行它时,它会显示“未将对象引用设置为对象的实例”。CSV阅读为空

这是我的代码...什么都有发生的整个过程

方法:

private void RunCSVGetToSqlOrder() 
    { 
     DAL d = new DAL(); 

     GetGeoLocations(); 

     string geoLocPath = Server.MapPath("~\\GeoLocations\\GeoLocation.csv"); 
     string assetListPath = Server.MapPath("~\\GeoLocations\\AssetList.csv"); 

     d.InsertAssetGeoLocation(ImportGeoLocations(geoLocPath)); 
     d.InsertAssetList(ImportAssetList(assetListPath)); 

     DeleteFileFromFolder(); 
    } 

获取CSV和保存到一个文件夹(工作):

private void GetGeoLocations() 
    { 
     string linkGeoLoc = "http://app03.gpsts.co.za/io/api/asset_group/[email protected][email protected]_token"; 
     string filepathGeoLoc = Server.MapPath("~\\GeoLocations\\GeoLocation.csv"); 

     using (WebClient wc = new WebClient()) 
     { 
      wc.DownloadFileAsync(new System.Uri(linkGeoLoc), filepathGeoLoc); 
     } 
    } 

读取csv文件并导入到sql:

private static DataTable ImportGeoLocations(string csvFilePath) 
    { 
     DataTable csvData = new DataTable(); 
     try 
     { 
      using (TextFieldParser csvReader = new TextFieldParser(csvFilePath)) 
      { 
       // csvReader.TextFieldType = FieldType.Delimited; 
       csvReader.SetDelimiters(new string[] { "," }); 
       csvReader.HasFieldsEnclosedInQuotes = true; 
       csvReader.TrimWhiteSpace = true; 

       string[] colFields = csvReader.ReadFields(); 

       foreach (string column in colFields) 
       { 
        DataColumn datecolumn = new DataColumn(column); 
        datecolumn.AllowDBNull = true; 
        csvData.Columns.Add(datecolumn); 
       } 
       while (!csvReader.EndOfData) 
       { 
        string[] fieldData = csvReader.ReadFields(); 
        //Making empty value as null 
        for (int i = 0; i < fieldData.Length; i++) 
        { 
         if (fieldData[i] == "") 
         { 
          fieldData[i] = null; 
         } 
        } 
        csvData.Rows.Add(fieldData); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
     } 
     return csvData; 
    } 

上面的代码给出了就行了“不设置到对象的实例对象引用”的错误,但是这是最可能是由于因为它是读取CSV为空(NULL)...

string[] colFields = csvReader.ReadFields(); 

我不知道我在做什么错...任何意见将不胜感激...

------------编辑--------- -----

下载后CSV文件如下所示:

enter image description here

--------解决方案------------

下面是解决方案:

 private void RunCSVGetToSqlOrder() 
    { 
     GetGeoLocations(); 
     DeleteFileFromFolder(); 
    } 

    private void GetGeoLocations() 
    { 
     string linkGeoLoc = "http://app03.gpsts.co.za/io/api/asset_group/[email protected][email protected]_token"; 
     string filepathGeoLoc = Server.MapPath("~\\GeoLocations\\GeoLocation.csv"); 

     using (WebClient wc = new WebClient()) 
     { 
      wc.DownloadFileAsync(new System.Uri(linkGeoLoc), filepathGeoLoc); 
      wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompletedGeoLoc); 
     } 

     string linkAssetList = "http://app03.gpsts.co.za/io/api/asset_group/[email protected][email protected]_token"; 
     string filepathAssetList = Server.MapPath("~\\GeoLocations\\AssetList.csv"); 

     using (WebClient wc = new WebClient()) 
     { 
      wc.DownloadFileAsync(new System.Uri(linkAssetList), filepathAssetList); 
      wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompletedAssetList); 
     } 
    } 

    void wc_DownloadFileCompletedGeoLoc(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
    { 
     DAL d = new DAL(); 

     string geoLocPath = Server.MapPath("~\\GeoLocations\\GeoLocation.csv"); 
     d.InsertAssetGeoLocation(ImportGeoLocations(geoLocPath)); 
    } 

    void wc_DownloadFileCompletedAssetList(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
    { 
     DAL d = new DAL(); 

     string assetListPath = Server.MapPath("~\\GeoLocations\\AssetList.csv"); 
     d.InsertAssetList(ImportAssetList(assetListPath)); 
    } 
+0

这是邪恶的:'catch(Exception ex) { }' –

+0

我还没有完成代码异常/ catch ...只要我能得到这个工作, – Kerieks

+0

但是不要用空的catch块开始编码。这使得找到缺陷更加困难。您至少可以在那里设置一个断点来查看补码消息和堆栈跟踪包含的行号。 –

回答

1

wc.DownloadFileAsync的调用返回Task对象,下载的文件仅在任务完成后才会完成。

此问题很难与调试器相媲美,因为当达到断点或稍后手动检查文件时,任务将有足够的时间完成。但是,如果代码运行时没有中断点,则在下载完成之前将会呼叫d.InsertAssetGeoLocation(ImportGeoLocations(geoLocPath)),因此csv文件将为空。

可能的解决方案:

  • 重新设计代码async/await
  • 使用wd.DownloadFileCompleted事件延续
  • 使用同步wd.DownloadFile方法

...只是仅举几例。

1

从截图,它看起来像CSV在计算机上未正确读取文件。取决于语言和区域设置,.CSV有时具有“;”作为分隔符而不是“,”。

您可以尝试手动将“;”替换为“;”并看看它是否解决了这个问题?

+0

谢谢你,改变角色的作品,它正在阅读文件,然后如何改变这在代码方面,而不是手动?对不起,这是我的第一次与这个工作...该文件将被更换每10分钟的最新地点的vihicles,因此我不能手动改变它每次...谢谢 – Kerieks