CSV读为空

时间:2016-07-25 08:20:27

标签: c# asp.net csv

我有一个页面,其间隔大约10分钟将csv文件上传到文件夹(由http链接接收)。这个csv文件必须上传到sql。我已经设法获取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/APIAssetGroupLocation.class?zqcEK60SxfoP4fVppcLoCXFWUfVRVkKS@auth_token@auth_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/APIAssetGroupLocation.class?zqcEK60SxfoP4fVppcLoCXFWUfVRVkKS@auth_token@auth_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/APIAssetGroupLocation.class?zqcEK60SxfoP4fVppcLoCXFWUfVRVkKS@auth_token@auth_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));
    }

2 个答案:

答案 0 :(得分:1)

从屏幕截图中看,您的计算机上无法正确读取CSV文件。根据语言和区域设置,.CSV有时会有&#34 ;;&#34;作为分隔符而不是&#34;,&#34;。

您可以尝试手动更换所有&#34;,&#34;用&#34;;&#34;并看看它是否解决了这个问题?

答案 1 :(得分:1)

The call to wc.DownloadFileAsync returns a Task object and the downloaded file is only complete after the Task is completed.

This issue is hard to catch with the debugger, because the task will have enough time to complete when a breakpoint is reached or the file is manually inspected later. However, when the code runs without break point, the call d.InsertAssetGeoLocation(ImportGeoLocations(geoLocPath)) will be reached before the download is complete and therefore the csv file will be empty.

Possible solutions:

  • Redesign the code with async / await
  • Use wd.DownloadFileCompleted event for continuation
  • Use the synchronous wd.DownloadFile method

... just to name a few.