Ich habe eine Seite, wo in Abständen von etwa 10 Minuten eine CSV-Datei in einen Ordner hochgeladen wird (erhalten durch einen http-Link). Diese CSV-Datei muss auf sql hochgeladen werden. Ich habe es geschafft, die CSV-Dateien zu bekommen und sie im Ordner zu speichern, aber das Problem, das ich habe, ist, dass, wenn ich versuche, die Daten zu lesen, dass die Datei leer ist (aber es ist nicht) ... Es wirft keine Fehler, aber wenn ich es mit einem Debug ausführen, zeigt es eine "Objektreferenz, die nicht auf eine Instanz eines Objekts festgelegt ist".CSV Lesen als leer
Dies ist mein Code ...
Methode von dem, was für diesen gesamten Prozess geschehen hat:
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();
}
die csv und zu in einen Ordner speichern (in Betrieb):
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);
}
}
Lesen Sie die CSV-Datei und importieren Sie sie in 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;
}
Der obige Code gibt den Fehler „Objektverweis auf eine Instanz eines Objekts nicht festgelegt“ auf der Linie, aber das ist wahrscheinlich auf Grund, weil es die csv als leer (null) ist zu lesen ...
string[] colFields = csvReader.ReadFields();
Ich bin nicht sicher, was ich falsch mache ... Jeder Rat würde sehr geschätzt werden ...
------------ EDIT --------- -----
die cSV-Datei nach dem Download sieht wie folgt aus:
-------- ------------ Lösung
Im Folgenden finden Sie die Lösung:
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));
}
Das ist böse: 'catch (Exception ex) { }' –
Ich habe den Code Ausnahme/Fang noch nicht gemacht ... werde den Teil tun, sobald ich das funktioniert .... – Kerieks
Beginnen Sie jedoch nicht mit leeren Catch-Blöcken zu codieren. Es macht es viel schwieriger, Fehler zu finden. Sie könnten dort zumindest einen Haltepunkt setzen, um die Comple-Nachricht und den Stacktrace einschließlich der Zeilennummer zu sehen. –