Connecting Tech Pros Worldwide Forums | Help | Site Map

How to save files in database....

Newbie
 
Join Date: Jul 2008
Posts: 28
#1: Oct 27 '08
Hi All,

How can i save any extension file type in the database and retreive that file when i want.

what i mean here that i have different file types like .txt,.doc,pdf etc.

how to save these file in database and retreive when needed and show it to user on request.

Is it possible ? can u please give me some sample code.



Thanks

-yanku

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Oct 27 '08

re: How to save files in database....


The column-type you will store it in depends on the database you are working on. MSSQL uses "Image", MySql uses "BLOB", Access uses "OLE Object".

But the basic idea is to use a FileStream to fill a Byte array (byte[]) and then use that byte array as a parameter for your insert query.
Newbie
 
Join Date: Jul 2008
Posts: 28
#3: Oct 27 '08

re: How to save files in database....


Hi thanks for your reply.

Can u please provide some code about how to convert file into bytes stream
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Oct 27 '08

re: How to save files in database....


No, because we're trying to help you help yourself.

However, I can give you a link to the MDSN Page for the FileStream object, and I can point you in the right direction:

Here's how to create a filestream object:
c#
Expand|Select|Wrap|Line Numbers
  1. using System.IO;
  2. .
  3. .
  4. .
  5. FileStream fs = new FileStream(@"c:\path\file.txt",FileMode.Open, FileAccess.Read);
  6.  
Change the path, FileMode, and FileAccess values as necessary, although for this task, this should be fine.

The method you will need to use to read the file's data is called "Read".
Reply