2013-01-15 84 views
-3

我不得不从分贝读大量BLOB数据(更然后300GB)并插入到另一个db.I正在通过使用以下代码如何以及何时在C#中处理对象?

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); 
} 

我逐行读取并插入数据在读取数据asnother db.The问题是内存完整的异常后发送一些数据生成,因为我不处置对象。 如何在单次迭代后处理对象?

+2

“我要读大量的BLOB数据(更多然后300GB)” - 嗯,WTF!你将不得不缓冲和读取块...最大的单个.NET对象是2GB。 –

+2

处理不是问题,除非你有300GB的内存,你不能在内存中读取那么多。 –

+1

处理对象与内存无关。它与释放非托管资源(如文件句柄)有关。 –

回答

1

为了配合一些答案和评论在一起,尝试这样的:

// 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); 
       } 
      } 
     } 
    } 
} 
0

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

+0

如何在使用语句中使用对象?它表明一个类应该是无可比拟的 – mck

+0

对不起,我的意思是连接对象,命令和数据记录器等。 – hesenger

+0

他的对象应该实现IDosposable以与'using'块一起使用。 – dotNETbeginner

相关问题