Solved

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

Posted on 2004-09-30
3
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

730 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