Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Connection String for Oracle database is not working 22 90
Not showing page correctly 3 29
Host asp.net pages 5 25
Error in script 11 45
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now