仅仅2.5M记录后,DataTable就会失去内存

时间:2016-09-21 06:35:37

标签: c# sql-server datatable out-of-memory sqldataadapter

我试图将数据从数据库带入dataTable,记录大约为600万。 我们知道DataTable Limit 16,777,216

参考:http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx

但是我的dataTable仅在250万条记录中耗尽了内存。我不知道为什么?

public DataTable Func(int totalRecords)
{
     SqlDependency.Start(ConfigurationManager.AppSettings["ConnectionString"].ToString());
     DataTable dt = new DataTable();

     SqlDataAdapter sda = new SqlDataAdapter();
     sda.SelectCommand = new SqlCommand();

     sda.SelectCommand.CommandText = "SP_NAME";
     sda.SelectCommand.Transaction = sqlTrans;
     sda.SelectCommand.Connection = this.connection;
     sda.SelectCommand.CommandType = CommandType.StoredProcedure;
     SqlCacheDependency dependency = new SqlCacheDependency(sda.SelectCommand);

     if (connection.State == ConnectionState.Closed)
         connection.Open();

     dt.Load(sda.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection));
     return dt;
 }

1 个答案:

答案 0 :(得分:0)

它是可执行文件吗?如果是这样,请确保

 Project Settings -> Build -> Prefer 32-bit

未选中