2011-03-03 44 views
9

我知道這種問題不時有人問到,但我找不到任何令人滿意的解決方案。Microsoft.ACE.OLEDB.12.0 CSV連接字符串

如何使用MS ACE OLEDB 12打開CSV文件? 我用下面的代碼嘗試它。

DbConnection connection = new OleDbConnection(); 
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\"Text;HDR=Yes\""; 
connection.Open(); 
DbCommand cmd; 

cmd = connection.CreateCommand(); 
cmd.CommandText = "SELECT * FROM [Mappe1#csv]"; 
DbDataReader reader = cmd.ExecuteReader(); 

while (reader.Read()) 
{ 
    for (int i = 0; i < reader.FieldCount; i++) 
     Console.Write("(" + reader.GetValue(i).ToString() + ")"); 

    Console.WriteLine(); 
} 

cmd.Dispose(); 
connection.Dispose(); 
Console.WriteLine("Done"); 
Console.ReadKey(); 

問題是僅找到一列。文本由';'分隔。即使當我用「Delimited(|)」f.e.指定分隔符時不起作用。

我找不到此提供任何文件...

+0

我們越來越遠離ACE。它有很多問題(幾乎沒有文檔,不支持,數據或工作表名稱中的特定字符的問題,...)。我們發現,如果您的設計乾淨,比使用Interop更快,更輕鬆,更可靠。 CSV的另外我們使用另一個API(LumenWorks CSV閱讀器:http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader) – SACO 2014-10-31 10:15:58

回答

0

嘗試:

connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\""; 

(插入 「FORMAT =分隔」 成連接字符串的擴展屬性...)

+0

不是;不用找了。我也試過FMT = TabDelimited(對於Tab文件)。 – SACO 2011-03-04 08:15:57

+0

很難說......那是我在項目中用來讀取csv的確切連接字符串,它的作用就像一個魅力。唯一的區別是在select語句中。我有「select * from file.csv」,直接指定文件名。我之前沒有看到「[mappe1#csv]」語法 - 是文件「mappe1#csv」的名稱還是隻是指定「mappe1.csv」的另一種方法?如果文件擴展名不是「.csv」,那麼以前它失敗了。 – blech 2011-03-04 14:44:55

+1

我會試一試。 mappe1#csv是由connection.GetSchema()提供的名稱。 – SACO 2011-03-15 09:48:24

6

幫我得到一個分號分隔的csv使用ACE.OLEDB.12.0在C#來解析: http://sqlserverpedia.com/blog/sql-server-bloggers/use-ace-drivers-and-powershell-to-talk-to-text-files/

的Cre吃在同一個目錄中的SCHEMA.INI文本文件作爲要與下列內容導入CSV文件:爲我工作

[fileIwantToImport.csv] 
Format=Delimited(;) 
ColNameHeader=True 

。但如此糟糕。

好像在連接字符串中的FORMAT=Delimited(;)已經過時了......

+2

Gross,但它的工作原理! – 2012-11-09 23:11:20

-1

你有沒有考慮創建數據集?

public static DataSet ConvertTabFiles(string File, string TableName, string delimiter) 
    { 
     //The DataSet to Return 
     DataSet result = new DataSet(); 

     //Open the file in a stream reader. 
     StreamReader s; 
     try 
     { 
      s = new StreamReader(@File); 
     } 
     catch 
     { 
      MessageBox.Show("Can't perform operation on file: " + File); 
      return result; 
     } 

     //Split the first line into the columns 
     string[] columns = null; 
     try 
     { 
      columns = s.ReadLine().Split(delimiter.ToCharArray()); 
     } 
     catch 
     { 
      MessageBox.Show("Can't parse the file " + File + ", please try again!"); 
      return result; 
     } 

     //Add the new DataTable to the RecordSet 
     result.Tables.Add(TableName); 
     //MessageBox.Show("Add the new DataTable to the RecordSet"); 

     //Cycle the colums, adding those that don't exist yet 
     //and sequencing the one that do. 
     foreach (string col in columns) 
     { 
      bool added = false; 
      string next = ""; 
      int i = 0; 
      while (!added) 
      { 
       //Build the column name and remove any unwanted characters. 
       string columnname = col + next; 

       //See if the column already exists 
       if (!result.Tables[TableName].Columns.Contains(columnname)) 
       { 
        //if it doesn't then we add it here and mark it as added 
        result.Tables[TableName].Columns.Add(columnname); 
        added = true; 
       } 
       else 
       { 
        //if it did exist then we increment the sequencer and try again. 
        i++; 
        next = "_" + i.ToString(); 
       } 
      } 
     } 

     //Read the rest of the data in the file.   
     string AllData = s.ReadToEnd(); 

     string[] rows = AllData.Split("\r\n".ToCharArray()); 

     //Now add each row to the DataSet   
     foreach (string r in rows) 
     { 
      //Split the row at the delimiter. 
      string[] items = r.Split(delimiter.ToCharArray()); 
      //Add the item 
      result.Tables[TableName].Rows.Add(r); 
     } 
     //Return the imported data. 
     return result; 
    } 
+0

這就像實現你自己的CSV-Reader。我們目前正在使用另一個CSV-Reader。 – SACO 2012-02-28 13:31:55

+1

這將失敗的數據與嵌入式分隔符,例如一個逗號應該是一個單一字段CSV文件。 – 2014-05-08 19:03:52