Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to insert any file like word or pdf.... as varchar into sqlserver.


how to insert file in varchar

please help me


i used this code but it hasn't worked

if (FileUpload1.FileName != "")
       {
           string filePath = Server.MapPath("~/pp/") + FileUpload1.FileName;
           string ext = Path.GetExtension(FileUpload1.FileName);

           switch (ext)
           {
               case ".doc":
                   contenttype = "application/vnd.ms-word";
                   break;

               case ".docx":
                   contenttype = "application/vnd.ms-word";
                   break;
           }
           FileUpload1.SaveAs(filePath);
           StreamReader myFile = new System.IO.StreamReader(filePath);
           myString = myFile.ReadToEnd();

           myFile.Close();




but it is inserting as in my database:-


????
Posted
Updated 7-Dec-11 19:22pm
v2
Comments
Abhinav S 8-Dec-11 1:22am    
Code tags added.

Varchar is for character data not for file bytes, for this you must use varbinary instead.

See here : http://www.daniweb.com/software-development/csharp/code/217419[^]
 
Share this answer
 
Comments
Member 7932936 8-Dec-11 2:09am    
i used varbinary but i want to insert as varchar
Mehdi Gholam 8-Dec-11 2:16am    
Well your wanting is in disagreement with the SQL Server.
Monjurul Habib 8-Dec-11 15:35pm    
5!
Mehdi Gholam 9-Dec-11 1:30am    
Cheers
Amir Mahfoozi 9-Dec-11 23:43pm    
+5 :)
try to use varbinary instead of varchar. varbinary helps you to save the content of the file.
--NDK
 
Share this answer
 
Comments
fjdiewornncalwe 8-Dec-11 14:07pm    
Please read the previous answers and do not repeat them. If you agree with an existing answer, vote that answer up and leave a note if you so choose.
varchar is for text/strings and not for file data types :)
 
Share this answer
 
Comments
fjdiewornncalwe 8-Dec-11 14:06pm    
Please don't repeat previous answers.
do you have any reason why you wanna save the file as varchar? I think that is possible but difference approach like you probably need to drop something in order to retain the text inside your file.

I think you need to use varbinary before releasing it to production, it is safer that way...
 
Share this answer
 
v2
Comments
Member 7932936 15-Dec-11 23:43pm    
because i want to do "full text index" search on files.

I have stored as varbinary

when i set full text index on my column it shows error as full text index only for varchar not varbinary .

that's why iam trying to insert as varchar
Since you are using SQL Server 2008, have you considered using FILESTREAM?

Here is an MSDN article that may be useful
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900