c# - How do you refill a byte array using SqlDataReader? -



c# - How do you refill a byte array using SqlDataReader? -

this in reference to: byte[] , efficiently passing reference

and sqldatareader found in post: getting binary info using sqldatareader

inside loop, i'm calling database , returning big object (varbinary[max]). currently, i'm running outofmemory exceptions, i'm trying cut down footprint in big object heap (loh).

so, i'm creating byte array largest file i'd download , adding padding in case. instance:

byte[] currentfile = new byte[largestfilesize * 1.1];

i pass currentfile database method. currently, utilize enterpriselibrary access database:

dbcommand storedprocedure = medicaredatabase.db.getstoredproccommand(spname); storedprocedure.commandtimeout = 5000; if (parameters != null) { foreach (param parameter in parameters) { if (parameter != null) { medicaredatabase.db.addinparameter(storedprocedure, parameter.parametername, parameter.dbtype, parameter.value); } } } seek { binarywriter bw; // streams blob filestream object. int buffersize = 100; // size of blob buffer. byte[] outbyte = new byte[buffersize]; // blob byte[] buffer filled getbytes. long retval; // bytes returned getbytes. long startindex = 0; // starting position in blob output. var myreader = medicaredatabase.db.executereader(storedprocedure); while (myreader.read()) { bw = new binarywriter(); // reset starting byte new blob. startindex = 0; // read bytes outbyte[] , retain number of bytes returned. retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize); // go on reading , writing while there bytes beyond size of buffer. while (retval == buffersize) { bw.write(outbyte); bw.flush(); // reposition start index end of lastly buffer , fill buffer. startindex += buffersize; retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize); } // write remaining buffer. bw.write(outbyte, 0, (int)retval - 1); bw.flush(); // close output file. bw.close(); }

this modification of code listed in sec article above.

here questions (and sense free right me if should asking different questions)

how efficiently refill byte[] without creating new object?

the above code doesn't utilize commandbehavior.sequentialaccess needed not create new object. how utilize enterpriselibrary commandbehaviors?

i'm calling database , returning have byte[] array

updated

so after time, i've decided manually populate byte array. reference beingness passed successfully.

sqlconnection pubsconn = null; sqlcommand logocmd = null; sqldatareader myreader = null; seek { pubsconn = new sqlconnection(system.configuration.configurationmanager.connectionstrings["medicareaccess"].connectionstring); logocmd = new sqlcommand("esmd.proc_ws_selectbiztalkbinary", pubsconn); logocmd.commandtype = commandtype.storedprocedure; sqlparameter submissionsetparamter = logocmd.parameters.add("@submissionsetid", sqldbtype.uniqueidentifier); submissionsetparamter.value = currentdocument.submissionsetid; sqlparameter filenameparam = logocmd.parameters.add("@filename", sqldbtype.varchar, 100); filenameparam.value = currentdocument.fullfilename; int buffersize = 100; // size of blob buffer. byte[] outbyte = new byte[buffersize]; // blob byte[] buffer filled getbytes. long retval; // bytes returned getbytes. long startindex = 0; // starting position in blob output. // open connection , read info datareader. pubsconn.open(); myreader = logocmd.executereader(commandbehavior.sequentialaccess); array.clear(data, 0, data.length); if (myreader == null) { return; } while (myreader.read()) { currentdocument.size = (int)myreader.getbytes(0, 0, null, 0, 0); int locationcounter = 0; // reset starting byte new blob. startindex = 0; // read bytes outbyte[] , retain number of bytes returned. retval = myreader.getbytes(0, startindex, outbyte, 0, buffersize); // go on reading , writing while there bytes beyond size of buffer. while (retval == buffersize) { (int = 0; < retval; i++) { data[locationcounter] = outbyte[i]; locationcounter++; } // reposition start index end of lastly buffer , fill buffer. startindex += buffersize; retval = myreader.getbytes(0, startindex, outbyte, 0, buffersize); } } } grab (exception ex) { throw ex; } { if (myreader != null) { myreader.dispose(); myreader.close(); myreader = null; } if (pubsconn != null) { pubsconn.dispose(); pubsconn.close(); pubsconn = null; } }

i'm sure there more efficient way write this. , hasn't been tested. reference working.

so replaced main while loop next code:

if (myreader.read()) { currentdocument.size = myreader.getbytes(0, 0, null, 0, 0); // reset starting byte new blob. long startindex = 0; int buffersize = 8196; // size of blob buffer. byte[] outbyte = new byte[buffersize]; // blob byte[] buffer filled getbytes. long bytesinbuffer = 0; // bytes returned getbytes. // go on reading , writing while there bytes beyond size of buffer. while (startindex < currentdocument.size) { bytesinbuffer = myreader.getbytes(0, startindex, outbyte, 0, buffersize); array.copy(outbyte, 0, currentdocument.data, startindex, bytesinbuffer); startindex += bytesinbuffer; } }

works now.

c# asp.net bytearray enterprise-library out-of-memory

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -