在c#中如何以及何时处置对象

本文关键字:何时处 对象 | 更新日期: 2023-09-27 17:52:59

我必须从数据库读取大量Blob数据(超过300Gb)并插入到另一个数据库。我正在使用以下代码读取数据

if (dr.HasRows)
{
    while (dr.Read())
    {
       media m = new media
       {
           docid = Convert.ToInt32(dr["Id"]),
           Content = Convert.ToByte(dr["BlobData"]),
           madiaName = Convert.ToString(dr["Name"])
       }
    }
    InsertInNewDb(m);
}

我正在逐行读取并在另一个数据库中插入数据。问题是在发送一些数据后产生内存满异常,因为我没有处理对象。如何在单次迭代后处理对象?

在c#中如何以及何时处置对象

要将许多答案和评论联系在一起,请尝试这样做:

// The SqlConnection, SqlCommand and SqlDataReader need to be in using blocks
// so that they are disposed in a timely manner. This does not clean  up
// memory, it cleans up unmanaged resources like handles
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM OldTable", conn))
    {
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                   media m = new media
                   {
                       // Don't convert - cast instead. These are already the correct
                       // type.
                       docid = (int) dr["Id"],
                       // There are more efficient ways to do this, but
                       // Convert.ToByte was copying only a single byte
                       Content = dr["BlobData"],
                       madiaName = (string)dr["Name"]
                   }
                    // You probably want to insert _all_ of the rows.
                    // Your code was only inserting the last
                    InsertInNewDb(m);
                }
            }
        }
    }
}

您可以尝试对DataReader进行分页,这应该可以工作。尝试在一些行之后关闭数据的连接、源和目标。为了更好地管理内存,请记住使用指令使用对象。