Click here to Skip to main content
14,585,119 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hallo Guys,

I want to build an app that stores for word document, pdf and other documents into a database. I don't want to save its path instead. When the doc is required, first I want to pull off it from the database and then save it into disk.

Does storing the doc file into a database brings a format change into the doc itself?

Thanks
Posted
Updated 17-Jul-13 23:15pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

No, provided you save it the right way.
If you use a binary column, and pass the document as a parameter (rather than trying to concatenate it into the command string) then it should work fine. (A VARCHAR column may work, but a binary could be smaller, and may prevent some conversion errors if you don;t know what the file type contains)
   
Comments
Yonathan1111 20-May-13 12:53pm
   
Thanks,
Here is my attempt, but it did not work out.

string ext = Path.GetExtension(@_path);
FileSecurity fSecurity = File.GetAccessControl(_path);

fSecurity.AddAccessRule(new FileSystemAccessRule(Environment.UserName,
FileSystemRights.WriteData, AccessControlType.Allow));

// Set the new access settings.

File.SetAccessControl(_path, fSecurity);


string file = String.Empty;

using (StreamReader sr = new StreamReader(File.Open(@_path, FileMode.Open)))
{
file = sr.ReadToEnd();

using (StreamWriter sw =
new StreamWriter(File.Create(@"Test.doc")))
{
sw.Write(file);
}
}
}
catch (Exception)
{

}
OriginalGriff 20-May-13 13:20pm
   
"it did not work out" is not very helpful - you need to stop swallowing exceptions and report them so you can see what is going wrong. Without the error, it is next to impossible to fix! :laugh:

Why are you reading with a Stream reader?
Try this:
byte[] data = File.ReadAllBytes(_path);
File.WriteAllBytes("Test.doc", data);
but give Test.Doc a path so it goes into a known folder rather the "current folder" which may not have sufficient access (it is the App folder by default).
And stop swollowing exceptions!
catch(Exception ex)
{
Console.Write(ex.ToString());
}
will at least tell you what the system s complaining about.
Yonathan1111 21-May-13 8:16am
   
Hi, Thanks a Lot.

It worked.
OriginalGriff 21-May-13 8:22am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100