【问题标题】:Microsoft.ACE.OLEDB.12.0 CSV ConnectionStringMicrosoft.ACE.OLEDB.12.0 CSV 连接字符串
【发布时间】:2011-07-08 03:42:00
【问题描述】:

我知道不时会提出此类问题,但我找不到任何令人满意的解决方案。

如何使用 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.它不会工作。

我找不到该提供商的任何文档...

【问题讨论】:

  • 我们越来越远离 ACE。它有很多问题(几乎没有文档,没有支持,数据或工作表名称中的特定字符问题......)。我们发现,如果您的设计简洁,则使用 Interop 比使用 ACE 更快、更容易、更可靠。对于 CSV,我们正在使用另一个 API(LumenWorks CSV 阅读器:codeproject.com/Articles/9258/A-Fast-CSV-Reader

标签: c# csv connection-string oledb provider


【解决方案1】:

这帮助我使用 ACE.OLEDB.12.0 获得了一个以分号分隔的 csv 以在 C# 中解析: http://sqlserverpedia.com/blog/sql-server-bloggers/use-ace-drivers-and-powershell-to-talk-to-text-files/:

在与要导入的 csv 文件相同的目录中创建一个 schema.ini 文本文件,内容如下:

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

为我工作。但是太恶心了。

好像连接字符串中的FORMAT=Delimited(;) 已经过时了...

【讨论】:

    【解决方案2】:

    试试:

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

    (在连接字符串的扩展属性中插入“FORMAT=Delimited”...)

    【讨论】:

    • 不;不用找了。我还尝试了 FMT=TabDelimited(用于 Tab 文件)。
    • 很难说...这就是我在项目中用来读取 csv 的确切连接字符串,它就像一个魅力。唯一的其他区别在于 select 语句。我有“select * from file.csv”,直接指定文件名。我之前没有见过“[mappe1#csv]”语法——是文件名“mappe1#csv”还是只是另一种指定“mappe1.csv”的方式?如果文件扩展名不是“.csv”,我之前已经失败了。
    • 我会试一试的。 mappe1#csv 是 connection.GetSchema() 将提供的名称。
    • 另一个问题:你的文件是什么格式的?用 ;? 分隔换行符为 \n 和哪种编码?我用 Excel Office 2007 生成了文件...
    【解决方案3】:

    您是否考虑过创建 DataSet?

        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;
        }
    

    【讨论】:

    • 这就像实现你自己的 CSV-Reader。我们目前正在使用另一个 CSV-Reader。
    • 这将在嵌入分隔符的数据上失败,例如CSV 文件中应为单个字段的内容中的逗号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2017-01-19
    相关资源
    最近更新 更多