2015-12-08 73 views
2

當我嘗試使用C#將csv文件上載到SQL Server表時,我收到了以下錯誤消息(csv文件沒有標題)。 錯誤消息:「名爲''的列已屬於此DataTable」使用C#將CSV文件上傳到sql server表 -

我試圖在網絡上的某個地方找到一些解決方案,但我真的被它困住了。 我的代碼:

SqlConnection con = new SqlConnection(@"server=.;Initial Catalog=myDtabase;Integrated Security=SSPI;"); 

      string filepath = @"c:\\my_CSV_file.csv"; 

      StreamReader sr = new StreamReader(filepath); 

      string line = sr.ReadLine(); 
      string[] value = line.Split(','); 
      DataTable dt = new DataTable(); 
      DataRow row; 

      foreach (string dc in value) 
      { 
       dt.Columns.Add(new DataColumn(dc)); 
      } 

      while (!sr.EndOfStream) 
      { 
       value = sr.ReadLine().Split(','); 
       if (value.Length == dt.Columns.Count) 
       { 
        row = dt.NewRow(); 
        row.ItemArray = value; 
        dt.Rows.Add(row); 
       } 
      } 

      SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock); 
      bc.DestinationTableName = "my_SQLServer_Table"; 
      bc.BatchSize = dt.Rows.Count; 
      con.Open(); 
      bc.WriteToServer(dt); 
      bc.Close(); 
      con.Close(); 
+2

是你的領域包括在雙引號'」「' – MethodMan

+0

你有沒有通過您的代碼使用調試器,看看您是否添加相同的列不止一次踩? –

+0

向我們展示CSV文件中的一些示例數據。前幾行也許。 –

回答

2

我覺得這個鏈接可以幫助你完成這件事。

http://forums.asp.net/t/1695615.aspx

像往常一樣,還有就是皮膚貓不止一種方法。所以,如果你不喜歡上面列出的解決方案,試試這個我知道會對你有用的腳本。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Data.SqlClient; 
using System.Data.OleDb; 
using System.Configuration; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      string server = "EXCEL-PC\\EXCELDEVELOPER"; 
      string database = "AdventureWorksLT2012"; 
      string SQLServerConnectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI", server, database); 


      string CSVpath = @"C:\Users\Ryan\Documents\Visual Studio 2010\Projects\Bulk Copy from CSV to SQL Server Table\WindowsFormsApplication1\bin"; // CSV file Path 
      string CSVFileConnectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};;Extended Properties=\"text;HDR=Yes;FMT=Delimited\";", CSVpath); 

      var AllFiles = new DirectoryInfo(CSVpath).GetFiles("*.CSV"); 
      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, "MyGroup"); 
         bulkCopy.ColumnMappings.Add(1, "ID"); 
         bulkCopy.ColumnMappings.Add(2, "Name"); 
         bulkCopy.ColumnMappings.Add(3, "Address"); 
         bulkCopy.ColumnMappings.Add(4, "Country"); 
         bulkCopy.DestinationTableName = "AllEmployees"; 
         bulkCopy.BatchSize = 0; 
         bulkCopy.WriteToServer(dt); 
         bulkCopy.Close(); 
        } 

       } 
       catch(Exception ex) 
        { 
         MessageBox.Show(ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
        } 
      } 
     } 
    } 
} 
+0

謝謝ryguy72! – ayoub

相關問題