简体   繁体   中英

C# Is it possible to set a memory limit size for a DataTable object?

I know DataTable has a Minimum Capacity Attribute but is there a Maximum Capacity Attribute? Where if a maximum size is reached while filling the object with data an exception is generated?

eg:

DataTable table = new DataTable();

using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
{
  SqlDataAdapter ds = new SqlDataAdapter(cmd);
  da.Fill(table); //generate exception if DataTable is filled with too much data (e.g. over 100mb in size)
}

Two part answer:

  1. The Minimum Capacity attribute is number of initially allocated data rows , and not related to bytes / MB used. One row could be 10 bytes or multiple MB, depending on what you are trying to load.
  2. .NET will allocate what it needs until it can't anymore (see System.OutOfMemoryException ), which I believe to be true for most if not all allocations. You can't limit this, unless you take control of all code and especially all allocations and start counting bytes, which I wouldn't recommend. I can't think of any classes in the Framework with support for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM