?
Solved

a c# byte array from a binary sql field - GetBytes

Posted on 2004-09-30
3
Medium Priority
?
483 Views
Last Modified: 2008-02-01
I want to read some binary data I have in sql.

first I make a reader from a sproc call:

Cmd = new SqlCommand("sp_get_binary", SqlConn);
Cmd.CommandType = CommandType.StoredProcedure;
reader = Cmd.ExecuteReader();

what's returned is a reader of 1 field and 1 row. I can get the data like this:

while ( reader.Read() )
{
  numBytes = reader.GetBytes(0, startIndex, byteArray, 0, bufferSize); // where startIndex=0 and bufferSize=100
}

now my the byte array "byteArray" is filled with stuff, but the wrong number of stuff. The GetBytes method uses the bufferSize, which must be assigned, to size the byte array returned.

What I want is the byte array sized to fit the returned value.

Question 1: Should I always set the buffer size of a GetBytes call to a value over the maximum possible return value? So like, should I set it to ten zillion if I think the returned array could be large?

The source below attempts to get a sized byte array:

MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);

while (numBytes == bufferSize)
{
  bw.Write(byteArray);
  bw.Flush();
  startIndex += bufferSize;
  numBytes = reader.GetBytes(1, startIndex, byteArray, 0, bufferSize);
}
bw.Write(keyBack, 0, (int)numBytesKey);
bw.Flush();

ms.Position = 0;
BinaryReader br = new BinaryReader(ms);
byteArray = br.ReadBytes( // why does this require a known length?

needless to say I don't know how to make this work exactly. What I need is a sized byte array from the DataReader, sized to the number of bytes returned by the db.

One of my biggest problems is I still don't completely "get" the how to use the various stream readers and writers to forge the structures I'm looking for. Why can't I easily get a sized byte array from a binaryreader? Is the MemoryStream object the fundamental stream holder for type conversions and such? Why am I running into many instances where I need to know the specific length of things I don't know the length of? Any resource links are greatly appreciated.
0
Comment
Question by:sethUSer420
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 12189095
From the help file on GetBytes:

"If you pass a buffer that is a null value, GetBytes returns the length of the field in bytes."

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12216457
Why a "C" grade?  Did that not provide you with a least a "B" grade answer?  What would have made that answer better?  Code examples?

This is my quality control survey, in order to provide better service in the future.

Bob
0
 

Author Comment

by:sethUSer420
ID: 12216551
well, I asked a few different things, had a pretty long and drawn out question, and the answer was short and I felt incomplete. I didn't get an answer for my first question labeled: question 1, and yes, I was hoping for some kind of code sample.

After reading your answer, it took me some fiddling to come up with the solution, which ultimately was to call getBytes twice, once for the length and again for the value. Your answer did lead me to a solution in the end though, which is why I felt the points were deserved.

Seth
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question