在从小型CSV文件创建newRows和向数据表添加数据时发生OOM异常

本文关键字:数据 添加 异常 OOM 数据表 小型 CSV 文件创建 newRows | 更新日期: 2023-09-27 18:07:18

下面的代码登录到一个https网站,获取一个cookie,然后导航到另一个页面以字符串形式下载CSV文件。然后将字符串拆分为一个数组,用于创建要添加到数据表中的datarow。一切正常,直到populateTable方法中的循环抛出内存不足异常。CSV文件只有206kb,大约500行,拆分后的CSV字符串的数组有16206个字符串。在Windows 7中观察资源监视器,我可以看到程序在崩溃之前慢慢地将其RAM大小增加到1.2 GB。

很遗憾,我不能分享网站的URL。

我不是真的在寻找帮助来解决这个问题,而是想知道为什么它会发生,因为CSV文件不是那么大。任何输入?

namespace GetSCInfo
{
    public partial class Form1 : Form
{
    DataTable dt = new DataTable("clientInfo");
    public Form1()
    {
        InitializeComponent();
        createTable();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        DateTime today = DateTime.Today;
        //Starting date for the query. the 2f is required
        string startDate = "01%2f01%2f2005";
        string endDate = today.Month.ToString() + "%" + today.Day.ToString() + "%" + today.Year.ToString();
        //Check the date to make sure 2 digits are used for the month and day. If only one is used then add a 0.
        string[] dateFormat = endDate.Split('%');
        if (dateFormat[0].Length < 2)
            dateFormat[0] = "0" + dateFormat[0];
        if (dateFormat[1].Length < 2)
            dateFormat[1] = "0" + dateFormat[1];
        endDate = dateFormat[0] + "%2f" + dateFormat[1] + "%2f" + dateFormat[2];
        //Values for the form on the website
        using (var client = new CookieAwareWebClient())
        {
            var values = new NameValueCollection
        {
            {"eid", ""},
            {"EmailAddress", "example@example.com"},
            {"Password", "myPassword"},
            {"chk_NotificationOnly", "false"},
            {"btn_login", "Login"},
        };
            //Login and get the cookie
            client.UploadValues("https://Website/loginpage", values);
            // If the previous call succeeded we now have a valid authentication cookie
            // so we could download the protected page
            var val2 = new NameValueCollection
            {
                {"StartDate", startDate},
                {"EndDate", endDate},
                {"ReportFormat", "CSV"},
            };
  string bytes = client.DownloadString("https://website/csvPage?StartDate=" + startDate + "&EndDate=" + endDate + "&ReportFormat=CSV");
            client.Dispose();
            populateTable(bytes);

        }

    }
 private void createTable()
 {
     dt.Columns.Add("Assists ID", typeof(string));
     dt.Columns.Add("Last Name", typeof(string));
     dt.Columns.Add("First Name", typeof(string));
     dt.Columns.Add("S/C Name", typeof(string));
     dt.Columns.Add("S/C Phone", typeof(string));
     dt.Columns.Add("S/C Email", typeof(string));
 }
private void populateTable(string data)
 {
     dt.Clear();
     string[] csvValues = data.Split(',');
     data = "";
    for(int i = 19; i < csvValues.Length - 19; i=+19)
    {
        DataRow row = dt.NewRow();
        row[0] = csvValues[i];
        row[1] = csvValues[i + 1];
        row[2] = csvValues[i + 2];
        row[3] = csvValues[i + 14];
        row[4] = csvValues[i + 15];
        row[5] = csvValues[i + 16];
        dt.Rows.Add(row);
        //dt.Rows.Add(csvValues[i], csvValues[i+1], csvValues[i+2], csvValues[i+14], csvValues[i+15], csvValues[i+16]);
    }
    dataGridView1.DataSource = dt;

 }
}
//Extend the Webclient
public class CookieAwareWebClient : WebClient
{
    private readonly CookieContainer m_container = new CookieContainer();
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = m_container;
        }
        return request;
    }
}

}

在从小型CSV文件创建newRows和向数据表添加数据时发生OOM异常

你会后悔的。在populateTable中,for的循环增量不增加。用i=+19代替i+=19。你只是将i设置为19,并不断地创建一个新的DataRow