Microsoft.ACE.OLEDB.12.0 CSV ConnectionString

时间:2011-03-03 15:26:59

标签: c# csv connection-string oledb provider

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

如何使用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指定分隔符。它不会起作用。

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

3 个答案:

答案 0 :(得分:6)

这有助于我使用ACE.OLEDB.12.0在C#中解析用分号分隔的csv: 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(;)已经过时了......

答案 1 :(得分:0)

尝试:

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

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

答案 2 :(得分:-1)

您是否考虑过创建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;
    }