2016-09-23 83 views
0

我試圖將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; 
    } 

} 
+0

是'CSVpath'有效的目錄嗎? –

+0

簡單'CSVpath'表示無效的目錄你不能調試,看看裏面有什麼 –

+0

@AmitKumarGhosh CSVpath從選擇文件的瀏覽按鈕中獲取目錄,在調試中顯示目錄到文件 – Stijn

回答

0

我敢肯定的例外是在此拋出行

var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ; 

它總是一個好東西包圍這try try語句周圍。 但是,由於異常消息說路徑不正確,所以它不存在或語法錯誤。

+0

感謝您的幫助,但我想知道語法有什麼問題,因爲我確信路徑存在。 – Stijn

+0

如果您可以發佈變量的內容。我可能會幫助你。 – iCode4U

+0

我認爲你的意思是'CSVpath'它從文本框中獲取他的內容,並且該文本框被一個選擇該文件的按鈕填充,因此它將成爲'C:\ Users \ s \ Desktop \ clients TEST.csv' – Stijn

相關問題