Image SQL type

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
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
 
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.
 
Back
Top