Getchunk & Appendchunk

  • Thread starter Thread starter Dan Reber
  • Start date Start date
D

Dan Reber

When I used VB6 & ADO I used GetChunk & AppendChunk to get/save large image
data to SQL Server. In ADO.Net that is not available but I found the below
link and it describes what to use as an alternative. Looking at the code I
don't see how the data in saved/retrieved in "chunks", is it done natively
in ADO.Net? If not, how should I do that now?

http://support.microsoft.com/kb/q309158/

Regards,

Dan
 
I figured out how. Below is the code I used for GetChunk replacement.

MemoryStream stream = new MemoryStream();
SqlParameter cmdParameter = new SqlParameter();
SqlCommand cmd = null;
SqlDataReader dr = null;
byte[] resultsArray = new byte[0];
string cmdText;
int bytesReturned;
int chunkSize = 65536;
int offSet = 0;

cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 0;
cmd.CommandText = cmdText;
cmd.Connection = pbiSQLConnection;
dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();

byte[] byteChunk = new byte[chunkSize];

do
{
bytesReturned = (int)dr.GetBytes(0, offSet, byteChunk, 0, chunkSize);
if (bytesReturned > 0)
stream.Write(byteChunk, 0, bytesReturned);

offSet += bytesReturned;

} while (bytesReturned == chunkSize);

return stream.ToArray();
 
I figured out how. Below is the code I used for GetChunk replacement.

MemoryStream stream = new MemoryStream();
SqlParameter cmdParameter = new SqlParameter();
SqlCommand cmd = null;
SqlDataReader dr = null;
byte[] resultsArray = new byte[0];
string cmdText;
int bytesReturned;
int chunkSize = 65536;
int offSet = 0;

cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 0;
cmd.CommandText = cmdText;
cmd.Connection = pbiSQLConnection;
dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();

byte[] byteChunk = new byte[chunkSize];

do
{
bytesReturned = (int)dr.GetBytes(0, offSet, byteChunk, 0, chunkSize);
if (bytesReturned > 0)
stream.Write(byteChunk, 0, bytesReturned);

offSet += bytesReturned;

} while (bytesReturned == chunkSize);

return stream.ToArray();

Thanks

Dan
 
Back
Top