Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
1.36/5 (4 votes)
See more:
How to save Image into folder(Server.MapPath("Temp\\" )) from sql database.
Posted
Updated 12-Jan-12 2:58am
v2

Once the image is saved in the database, you can retreive the image and save in a file as follows:

C#
SqlCommand mycmd = new SqlCommand("select image_field from your_table where field_id = @ID",yourSqlConn);
cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);
cmdSelect.Parameters["@ID"].Value = 100; //this can be also an input for user etc.

yourSqlConn.Open();
byte[] img=(byte[])cmdSelect.ExecuteScalar();
string strfn=Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs=new FileStream(strfn,FileMode.CreateNew, FileAccess.Write);
fs.Write(img,0,img.Length);
fs.Flush();
fs.Close();


check the following article for more details:
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]
 
Share this answer
 
Hi you can save like this,

C#
string fname= System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string activeDir = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Temp";
System.IO.Directory.CreateDirectory(activeDir);
string newPath = System.IO.Path.Combine(activeDir, fname);
FileUpload1.PostedFile.SaveAs(newPath);
 
Share this answer
 
Comments
Robymon 12-Jan-12 4:31am    
I need a solution from sql to folder. i know this.
Hi...
How to store & retrieve images in mysql database using asp.net c#[^]

for saving into folder:
C#
//Get Filename from fileupload control
                string filename = Path.GetFileName(imgful.PostedFile.FileName);
                //Save images into foldername folder
                imgful.SaveAs(Server.MapPath("~/foldername/" + filename));

and write ur insert query.

thank u.
 
Share this answer
 
v3
Comments
CHill60 7-Jun-13 10:19am    
If you have a question then post it as a question, not as a solution to a different question that is over a year old
If you have a limited number of images in the table you can go with the solution that need to iterate all the rows, read image bytes and write them on the disc. But this approach will be extremely expensive in case you have a large number of images. In this condition, you should use SSIS. Following is a step by step tutorial on how to do that:

http://www.mssqltips.com/sqlservertip/2693/export-images-from-a-sql-server-table-to-a-folder-with-ssis/[^]
 
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