2016-07-25 236 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