gpt4 book ai didi

c# - 读取大制表符分隔的 txt 文件的有效方法?

转载 作者:太空狗 更新时间:2023-10-29 22:24:15 25 4
gpt4 key购买 nike

我有一个包含 50 万条记录的制表符分隔的 txt 文件。我正在使用下面的代码将数据读取到数据集。 50K 时它工作正常,但 500K 时它给出“抛出类型为‘System.OutOfMemoryException’的异常。”

读取大型制表符分隔数据的更有效方法是什么?或者如何解决这个问题?请举个例子

public DataSet DataToDataSet(string fullpath, string file)
{
string sql = "SELECT * FROM " + file; // Read all the data
OleDbConnection connection = new OleDbConnection // Connection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";"
+ "Extended Properties=\"text;HDR=YES;FMT=Delimited\"");
OleDbDataAdapter ole = new OleDbDataAdapter(sql, connection); // Load the data into the adapter
DataSet dataset = new DataSet(); // To hold the data
ole.Fill(dataset); // Fill the dataset with the data from the adapter
connection.Close(); // Close the connection
connection.Dispose(); // Dispose of the connection
ole.Dispose(); // Get rid of the adapter
return dataset;
}

最佳答案

使用流方法 TextFieldParser - 这样你就不会一次性将整个文件加载到内存中。

关于c# - 读取大制表符分隔的 txt 文件的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6051275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com