Here's an example for reading data in chunks:
using (var conn = new SqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "select somebinary from mytable where id = 1";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
byte[] buffer = new byte[1024]; // Read chunks of 1KB
long bytesRead = 0;
long dataIndex = 0;
while ((bytesRead = reader.GetBytes(0, dataIndex, buffer, 0, buffer.Length)) > 0)
{
byte[] actual = new byte[bytesRead];
Array.Copy(buffer, 0, actual, 0, bytesRead);
// TODO: Do something here with the actual variable,
// for example write it to a stream
dataIndex += bytesRead;
}
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…