以下示例从 microsoft sql server 中的 pubs 示例数据库中返回发行者 id 和徽标。发行者 id (pub_id) 是字符字段,而徽标则是图形,即 blob。请注意,由于必须按顺序访问字段,所以将在访问徽标之前访问当前数据行的发行者 id。
[visual basic] dim pubsconn as sqlconnection = new sqlconnection("data source=localhost;integrated security=sspi;initial catalog=pubs;") dim logocmd as sqlcommand = new sqlcommand("select pub_id, logo from pub_info", pubsconn)
dim fs as filestream ' writes the blob to a file (*.bmp). dim bw as binarywriter ' streams the binary data to the filestream object.
dim buffersize as integer = 100 ' the size of the blob buffer. dim outbyte(buffersize - 1) as byte ' the blob byte() buffer to be filled by getbytes. dim retval as long ' the bytes returned from getbytes. dim startindex as long = 0 ' the starting position in the blob output.
dim pub_id as string = "" ' the publisher id to use in the file name.
' open the connection and read data into the datareader. pubsconn.open() dim myreader as sqldatareader = logocmd.executereader(commandbehavior.sequentialaccess)
do while myreader.read() ' get the publisher id, which must occur before getting the logo. pub_id = myreader.getstring(0)
' create a file to hold the output. fs = new filestream("logo" & pub_id & ".bmp", filemode.openorcreate, fileaccess.write) bw = new binarywriter(fs)
' reset the starting byte for a new blob. startindex = 0
' read bytes into outbyte() and retain the number of bytes returned. retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize)
' continue reading and writing while there are bytes beyond the size of the buffer. do while retval = buffersize bw.write(outbyte) bw.flush()
' reposition the start index to the end of the last buffer and fill the buffer. startindex = startindex + buffersize retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize) loop
' write the remaining buffer. bw.write(outbyte) bw.flush()
' close the output file. bw.close() fs.close() loop
' close the reader and the connection. myreader.close() pubsconn.close() [c#] sqlconnection pubsconn = new sqlconnection("data source=localhost;integrated security=sspi;initial catalog=pubs;"); sqlcommand logocmd = new sqlcommand("select pub_id, logo from pub_info", pubsconn);
filestream fs; // writes the blob to a file (*.bmp). binarywriter bw; // streams the blob to the filestream object.
int buffersize = 100; // size of the blob buffer. byte[] outbyte = new byte[buffersize]; // the blob byte[] buffer to be filled by getbytes. long retval; // the bytes returned from getbytes. long startindex = 0; // the starting position in the blob output.
string pub_id = ""; // the publisher id to use in the file name.
// open the connection and read data into the datareader. pubsconn.open(); sqldatareader myreader = logocmd.executereader(commandbehavior.sequentialaccess);
while (myreader.read()) { // get the publisher id, which must occur before getting the logo. pub_id = myreader.getstring(0);
// create a file to hold the output. fs = new filestream("logo" + pub_id + ".bmp", filemode.openorcreate, fileaccess.write); bw = new binarywriter(fs);
// reset the starting byte for the new blob. startindex = 0;
// read the bytes into outbyte[] and retain the number of bytes returned. retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize);
// continue reading and writing while there are bytes beyond the size of the buffer. while (retval == buffersize) { bw.write(outbyte); bw.flush();
// reposition the start index to the end of the last buffer and fill the buffer. startindex+= buffersize; retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize); }
// write the remaining buffer. bw.write(outbyte); bw.flush();
// close the output file. bw.close(); fs.close(); }
// close the reader and the connection. myreader.close(); pubsconn.close();