我有一个页面,大约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文件如下所示:
--------解决方案------------
下面是解决方案:
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));
}
这是邪恶的:'catch(Exception ex) { }' –
我还没有完成代码异常/ catch ...只要我能得到这个工作, – Kerieks
但是不要用空的catch块开始编码。这使得找到缺陷更加困难。您至少可以在那里设置一个断点来查看补码消息和堆栈跟踪包含的行号。 –