Ich versuche eine CSV-Datei in eine SQL Server-Datenbank zu importieren. Aber wenn ich die Datei auswählen bricht es bei var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ;
mit einer Fehlermeldung:Eine nicht behandelte Ausnahme vom Typ 'System.IO.IOException' ist in mscorlib.dll aufgetreten. Der Verzeichnisname ist ungültig.
Eine nicht behandelte Ausnahme des Typs ‚System.IO.IOException‘ ist in mscorlib.dll aufgetreten Der Verzeichnisname ist ungültig.
(beachten Sie den „Der Verzeichnisname ist ungültig.“ wird von dem niederländischen Satz übersetzt „De mapnaam ist ongeldig.“)
string server = "LOCALHOST";
string database = "klantbestand";
string SQLServerConnectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=true", server, database);
string CSVpath = txtfilePath.Text; // CSV file Path
string CSVFileConnectionString = String.Format(CSVpath);
var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ;
string File_Name = string.Empty;
foreach (var file in AllFiles)
{
try
{
DataTable dt = new DataTable();
using (OleDbConnection con = new OleDbConnection(CSVFileConnectionString))
{
con.Open();
var csvQuery = string.Format("select * from [{0}]", file.Name);
using (OleDbDataAdapter da = new OleDbDataAdapter(csvQuery, con))
{
da.Fill(dt);
}
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLServerConnectionString))
{
bulkCopy.ColumnMappings.Add(0, "Clientnr");
bulkCopy.ColumnMappings.Add(1, "contact");
bulkCopy.ColumnMappings.Add(2, "company");
bulkCopy.ColumnMappings.Add(3, "address");
bulkCopy.ColumnMappings.Add(4, "zipcode");
bulkCopy.ColumnMappings.Add(5, "phone");
bulkCopy.ColumnMappings.Add(6, "mobile");
bulkCopy.ColumnMappings.Add(7, "email");
bulkCopy.ColumnMappings.Add(8, "taxnumber");
bulkCopy.ColumnMappings.Add(9, "BIC");
bulkCopy.ColumnMappings.Add(10, "Bank");
bulkCopy.ColumnMappings.Add(11, "SendMethod");
bulkCopy.ColumnMappings.Add(12, "Active");
bulkCopy.ColumnMappings.Add(13, "Notes");
bulkCopy.ColumnMappings.Add(14, "PaymentMethod");
bulkCopy.ColumnMappings.Add(15, "Mandate");
bulkCopy.ColumnMappings.Add(16, "MandatDate");
bulkCopy.ColumnMappings.Add(17, "CollectionType");
bulkCopy.ColumnMappings.Add(18, "Country");
bulkCopy.ColumnMappings.Add(19, "EmailIntro");
bulkCopy.ColumnMappings.Add(20, "PaymentPeriod");
bulkCopy.ColumnMappings.Add(21, "Reference");
bulkCopy.DestinationTableName = "GegevensCSV";
bulkCopy.BatchSize = 0;
bulkCopy.WriteToServer(dt);
bulkCopy.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Title = "kies csv file";
filedialog.Filter = "csv Files|*.csv";
filedialog.InitialDirectory = @"c:\";
if(filedialog.ShowDialog()== DialogResult.OK)
{
txtfilePath.Text = filedialog.FileName;
}
}
ist 'CSVpath' ein gültiges Verzeichnis? –
Simple 'CSVpath' steht für ungültiges Verzeichnis. Sie können nicht debuggen und sehen, was drin ist. –
@AmitKumarGhosh CSVpath holt das Verzeichnis von einer Browse-Schaltfläche, die eine Datei auswählt, im Debug zeigt es das Verzeichnis zur Datei – Stijn