我嘗試使用「|」讀取文本文件數據,分開,我使用下面的代碼。試圖把數據表中的數據到數據視圖我m到處類型的異常時,我爲能夠讀取後,讀取文本文件中的數據「System.OutOfMemoryException的」被拋出,從文本文件讀取數據時拋出類型'System.OutOfMemoryException'的異常
任何一個可以建議我如何避免此異常。
string filepath = System.Configuration.ConfigurationManager.AppSettings["data"];
if (filepath != "")
{
DataTable dt = new DataTable("file");
string[] columns1 = null;
var lines = File.ReadAllLines(filepath);
int Count = lines.Length;
//here taking columns and adding to table
if (lines.Count() > 0)
{
columns1 = lines[0].Split(new char[] { '|' });
foreach (var column in columns1)
dt.Columns.Add(column);
}
for (int i = 1; i < lines.Count(); i++)
{
DataRow dr = dt.NewRow();
string[] values = lines[i].Split(new char[] { '|' });
for (int j = 0; j < values.Count() && j < columns1.Count(); j++)
{
dr[j] = values[j];
}
}
dt.Rows.Add(dr);
DataView View = new DataView(dt);
//Here I m getting "Exception of type 'System.OutOfMemoryException' was thrown."
DataTable MD = View.ToTable("MD", false, "ID", "Description")
DataTable MM = View.ToTable("MM", false, "RecordNumber", "Item description")
if (MD.Rows.Count > 0)
{
InsertData(MD);
}
if (MM.Rows.Count > 0)
{
InsertData1(MM);
}
}
堆棧跟蹤: - String.Split的
at System.Collections.Generic.List`1.set_Capacity(Int32 value)
at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
at System.Collections.Generic.List`1.Add(T item)
at System.Data.DataView.ToTable(String tableName, Boolean distinct, String[] columnNames)
什麼是一個文件的大小? – wudzik
異常行(完整跟蹤)。該問題很可能是文件大小,或文件中的條目數超過了列表中允許的最大條目數。 – Kilazur
我試圖讀取有5記錄的文本文件。 –