Image SQL type

G

Guest

Hi,
I would like to read a file from the File System and save it into a SQL
Server field, which is a image type. Somebody told to me that it is possible
to read the file with StreamReader and save all the byte into an array
(byte[]), but I don't know how to code it in C#.

Can anybody help me, please
Thank you
 
M

Morten Wennevik

Hi Sio,

To get all the bytes from a file, just use

using(FileStream fs = File.OpenRead("image.jpg"))
{
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
}

You can feed the byte[] to a binary storage field in the database.
 
G

Guest

Thank you Morten,
do you think that it's possible to read the format of the file? namely,
through the instance of FileStream, is it possible to read the format file?
As format file, I mean a Word, Excel, Notepad, Acrobat, etc. file... My aim
is to avoid reading the extention of the file name... is there a solution?

Thanks again
 
M

Morten Wennevik

There isn't a simple way to identify what kind of file it is just by reading the FileStream. You would have to do it manually for each file type and perhaps many times for each file extension too. It would be far easier to just store the file extension.

Thank you Morten,
do you think that it's possible to read the format of the file? namely,
through the instance of FileStream, is it possible to read the format file?
As format file, I mean a Word, Excel, Notepad, Acrobat, etc. file... My aim
is to avoid reading the extention of the file name... is there a solution?

Thanks again

Morten Wennevik said:
Hi Sio,

To get all the bytes from a file, just use

using(FileStream fs = File.OpenRead("image.jpg"))
{
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
}

You can feed the byte[] to a binary storage field in the database.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top