2011-03-03 118 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