Varbinary to file

Hoongz

Active member
Joined
Nov 22, 2007
Messages
32
Programming Experience
1-3
How do i write the binary i retrieve from sql into files which i can append into WMP control? Need help urgently. Anyone here is able to help or direct me to any site/forum please. All helps greatly appriciated. Thanks and regards
 
A VarBinary field contains a Byte array. You can write binary data to a file using a FileStream.

If your binary data was read from a file whose format is supported by WMP then when you write it back to a file that will be playable too. If the data doesn't represent a format supported by WMP then it won't.
 
How do i write back? Can i get some sample data? And how does the file stream knows what file extension it is? Oh thank god you replied. My project is due this week and i'm dying from stress.
 
The FileStream doesn't know or care anything about file extensions. When you open a FileStream you specify the path of the file to open or create. If you want some sample data then you'd need to open an existing media file, also using a FileStream. I suggest that you read the MSDN documentation for the FileStream class.

That said, you may find it quicker and easier to just use the File.ReadAllBytes and .WriteAllBytes methods. One will read an entire file into a Byte array in one line of code, while the other will write an entire Byte array to a file in one line. If it's a large file the array will be large, but you're likely to have to deal with the entire file's contents anyway so that's no big deal.
 
So what you are saying is if i declare the path as a .jpg extension, it will be a jpeg file right? Thank you for the help.
 
No, I'm not saying anything of the sort. If you have a box of bananas and you write "Elephant" on it, does that mean that it contains an elephant? You can give any file any extension you want and it doesn't make a bit of difference to what's in the file.

If a file has a JPG extension and you double-click it in Windows Explorer then the system will assume that it's a JPG file and try to open it in the default imaging app. If that file was actually a Word document that you changed the extension of then the app will fail to open the file, as you'd expect.

What I'm saying is that, in response to this:
And how does the file stream knows what file extension it is?
the FileStream does NOT know what extension the file should have. The FileStream doesn't care; all it sees are bytes. It will give the file whatever extension you tell it to. It's YOUR data. It's up to YOU to know what the appropriate extension is.
 
Back
Top