我試圖將CSV文件導入到SQL服務器數據庫中。但是,當我選擇文件它打破在var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ;
一個錯誤信息:在mscorlib.dll中發生未處理的「System.IO.IOException」類型異常目錄名稱無效
類型的未處理的異常「System.IO.IOException」出現在mscorlib.dll目錄名稱是無效的。
(注意這個「目錄名稱是無效的。」 已經從荷蘭的句子翻譯成「德mapnaam是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;
}
}
是'CSVpath'有效的目錄嗎? –
簡單'CSVpath'表示無效的目錄你不能調試,看看裏面有什麼 –
@AmitKumarGhosh CSVpath從選擇文件的瀏覽按鈕中獲取目錄,在調試中顯示目錄到文件 – Stijn