Microsoft.ACE.OLEDB.12.0 CSV 连接字符串

发布于 2024-10-20 10:11:14 字数 794 浏览 7 评论 0原文

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

如何使用 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(|)”指定分隔符,它也不起作用。

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

I know questions this kind are asked from time to time but i can't find any satisfying solution.

How can I open a CSV-File using MS ACE OLEDB 12?
I try it with the following code.

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();

The Problem is that only one column is found. The Text is delimited by ';'. Even when i specifiy the delimiter with "Delimited(|)" f.e. it will not work.

I can't find any documentation for this provider...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-10-27 10:11:14

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

创建schema.ini 与要导入的 csv 文件位于同一目录中的文本文件,其中包含以下内容:

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

为我工作。但太恶心了。

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

This helped me getting a semicolon-delimited csv to parse in C# using ACE.OLEDB.12.0:
http://sqlserverpedia.com/blog/sql-server-bloggers/use-ace-drivers-and-powershell-to-talk-to-text-files/:

Create a schema.ini text file in the same directory as the csv file you want to import with the following contents:

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

Worked for me. But so yucky.

Seems like the FORMAT=Delimited(;) in the connection string has gone out of fashion...

踏月而来 2024-10-27 10:11:14

尝试:(

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

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

Try:

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

(inserting "FORMAT=Delimited" into the extended properties of the connection string...)

千鲤 2024-10-27 10:11:14

您是否考虑过创建数据集?

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

Have you considered creating 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文