Solved

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

Posted on 2004-09-30
3
471 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

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

705 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