将逗号分隔的文本文件读取到C#DataTable,列被截断为255个字符

时间:2009-06-26 21:01:27

标签: c# .net csv oledb jet

我们正在从CSV导入到SQL。为此,我们正在读取CSV文件并使用schema.ini写入临时.txt文件。 (我不确定为什么要写这个临时文件,但这就是代码当前的工作方式)。从那里,我们使用以下连接字符串(对于ASCII文件)通过OleDB加载DataTable。

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath + ";Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";

我们遇到的问题是超过255个字符的字段会被截断。我在网上看过这个问题,看来默认情况下,文本字段会被截断。

我在ImportMixedTypes=Majority Type中设置了我的注册表设置TypeGuessRows=0HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel ,希望mycolumns不再被解释为文本。执行此操作后,临时txt文件正在从CSV文件中正确写入,但是当我调用dataAdapter.Fill时,生成的DataTable仍然具有截断值。

以下是相关的列定义。   CommaDelimited #txt注释2错误234真130 0 0

任何帮助将不胜感激。目前,我对使用任何3d派对代码解决这个问题不感兴趣,必须有一种方法可以使用内置工具。

以下是表格定义:

<Columns> 
    <TABLE_NAME>CommaDelimited#txt</TABLE_NAME> 
    <COLUMN_NAME>Notes</COLUMN_NAME> 
    <ORDINAL_POSITION>2</ORDINAL_POSITION> 
    <COLUMN_HASDEFAULT>false</COLUMN_HASDEFAULT> 
    <COLUMN_FLAGS>234</COLUMN_FLAGS> 
    <IS_NULLABLE>true</IS_NULLABLE> 
    <DATA_TYPE>130</DATA_TYPE> 
    <CHARACTER_MAXIMUM_LENGTH>0</CHARACTER_MAXIMUM_LENGTH> 
    <CHARACTER_OCTET_LENGTH>0</CHARACTER_OCTET_LENGTH> 
</Columns>

谢谢,

格雷格


我尝试编辑schema.ini指定宽度的文本,但没有帮助(之前设置为备忘录)

[CommaDelimited.txt] 格式= CSVDelimited DecimalSymbol =。 Col1 = Notes Text Width 5000

6 个答案:

答案 0 :(得分:6)

这是一个简单的类,用于读取分隔文件并返回不截断字符串的DataTable(所有字符串)。如果列名不在文件中,它有一个重载方法来指定列名。也许你可以使用它?

导入的命名空间

using System;
using System.Text;
using System.Data;
using System.IO;

代码

/// <summary>
/// Simple class for reading delimited text files
/// </summary>
public class DelimitedTextReader
{
    /// <summary>
    /// Read the file and return a DataTable
    /// </summary>
    /// <param name="filename">File to read</param>
    /// <param name="delimiter">Delimiting string</param>
    /// <returns>Populated DataTable</returns>
    public static DataTable ReadFile(string filename, string delimiter)
    {
        return ReadFile(filename, delimiter, null);
    }
    /// <summary>
    /// Read the file and return a DataTable
    /// </summary>
    /// <param name="filename">File to read</param>
    /// <param name="delimiter">Delimiting string</param>
    /// <param name="columnNames">Array of column names</param>
    /// <returns>Populated DataTable</returns>
    public static DataTable ReadFile(string filename, string delimiter, string[] columnNames)
    {
        //  Create the new table
        DataTable data = new DataTable();
        data.Locale = System.Globalization.CultureInfo.CurrentCulture;

        //  Check file
        if (!File.Exists(filename))
            throw new FileNotFoundException("File not found", filename);

        //  Process the file line by line
        string line;
        using (TextReader tr = new StreamReader(filename, Encoding.Default))
        {
            //  If column names were not passed, we'll read them from the file
            if (columnNames == null)
            {
                //  Get the first line
                line = tr.ReadLine();
                if (string.IsNullOrEmpty(line))
                    throw new IOException("Could not read column names from file.");
                columnNames = line.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
            }

            //  Add the columns to the data table
            foreach (string colName in columnNames)
                data.Columns.Add(colName);

            //  Read the file
            string[] columns;
            while ((line = tr.ReadLine()) != null)
            {
                columns = line.Split(new string[] { delimiter }, StringSplitOptions.None);
                //  Ensure we have the same number of columns
                if (columns.Length != columnNames.Length)
                {
                    string message = "Data row has {0} columns and {1} are defined by column names.";
                    throw new DataException(string.Format(message, columns.Length, columnNames.Length));
                }
                data.Rows.Add(columns);
            }
        }
        return data;

    }
}

必需的命名空间

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;

以下是调用它并上传到SQL数据库的示例:

        Stopwatch sw = new Stopwatch();
        TimeSpan tsRead;
        TimeSpan tsTrunc;
        TimeSpan tsBcp;
        int rows;
        sw.Start();
        using (DataTable dt = DelimitedTextReader.ReadFile(textBox1.Text, "\t"))
        {
            tsRead = sw.Elapsed;
            sw.Reset();
            rows = dt.Rows.Count;
            string connect = @"Data Source=.;Initial Catalog=MyDB;Integrated Security=SSPI";
            using (SqlConnection cn = new SqlConnection(connect))
            using (SqlCommand cmd = new SqlCommand("TRUNCATE TABLE dbo.UploadTable", cn))
            using (SqlBulkCopy bcp = new SqlBulkCopy(cn))
            {
                cn.Open();
                sw.Start();
                cmd.ExecuteNonQuery();
                tsTrunc = sw.Elapsed;
                sw.Reset();

                sw.Start();
                bcp.DestinationTableName = "dbo.UploadTable";
                bcp.ColumnMappings.Add("Column A", "ColumnA");
                bcp.ColumnMappings.Add("Column D", "ColumnD");
                bcp.WriteToServer(dt);
                tsBcp = sw.Elapsed;
                sw.Reset();
            }
        }

        string message = "File read:\t{0}\r\nTruncate:\t{1}\r\nBcp:\t{2}\r\n\r\nTotal time:\t{3}\r\nTotal rows:\t{4}";
        MessageBox.Show(string.Format(message, tsRead, tsTrunc, tsBcp, tsRead + tsTrunc + tsBcp, rows));

答案 1 :(得分:1)

如果要求它根据备忘录处理数据,Jet数据库引擎会截断备注字段:聚合,重复数据删除,格式化等。

http://allenbrowne.com/ser-63.html

答案 2 :(得分:0)

您可以通过正确指定schema.ini文件来解决此问题。我相信这两个选项是将列设置为备注类型,或设置宽度&gt; 255。

答案 3 :(得分:0)

我倾向于在读取CSV文件时直接创建DataTable,而不是通过额外的步骤将数据写入不同的文本文件,只是再次将其读回内存。

就此而言,您最终如何将DataTable中的数据导入SQL数据库?如果您只是循环访问DataTable并执行一堆INSERT语句,为什么不跳过两个中间人并在您最初读取CSV文件时调用相同的INSERT语句?

答案 4 :(得分:0)

我认为最好的方法是在以下博客中使用CSVReader: http://ronaldlemmen.blogspot.com/2008/03/stopping-and-continuing-save-event.html

答案 5 :(得分:-1)

我的.Net代码遇到了类似的问题,我只能通过将原始帖子中提到的注册表设置更改为Majortity Type来使其工作。在我的案例中,我做的不同之处是: 由于我们尝试从CSV而不是Excel导入。所以我不得不更改注册表中的文本设置(非Excel)。

尝试这样做,我认为它应该有效