Solved

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

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

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…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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