Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

Is there any possible to store the images in sql server data base.

Thank U.
Posted

 
Share this answer
 
Comments
Espen Harlinn 11-Jul-12 9:18am    
5'ed!
Mehdi Gholam 11-Jul-12 9:27am    
Cheers Espen!
Manas Bhardwaj 11-Jul-12 9:31am    
Correct +5!
Mehdi Gholam 11-Jul-12 9:41am    
Thanks Manas!
Does google stops working?

Try search on google[^] first.

You can also try search on CodeProject Articles[^] or CodeProject QA[^]

This is simple code snippets which stores image in SQL using FileUpload control
C#
FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
            BinaryReader str1 = new BinaryReader(file);
            byte[] bit = str1.ReadBytes(Convert.ToInt32( file.Length));
            SqlConnection connection = new SqlConnection("server=(local);database=test;integrated security=true");
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@Photo",bit);
            command.CommandText = "INSERT INTO image1(pic) VALUES (@Photo)";
            command.ExecuteNonQuery();
            connection.Close();
 
Share this answer
 
Comments
Mehdi Gholam 11-Jul-12 9:27am    
5'ed
Prasad_Kulkarni 11-Jul-12 23:51pm    
Thank you Mehdi!
Manas Bhardwaj 11-Jul-12 9:31am    
Correct +5!
Prasad_Kulkarni 11-Jul-12 23:51pm    
Thank you Manas!
You want to store your image file in database o.k here you go:-
first take a FileUpload Control provide the Id of this control say fileUpload.
now take a button on button_Click write the following code:-

C#
int length = fileUpload.PostedFile.ContentLength;
byte[] imgByte = new byte[length];
HttpPostedFile hpf = fileUpload.PostedFile;
hpf.InputStream.Read(imgByte, 0, length);


Now insert imgByte object into your database.
Remember datatype of your database column for Image must of type Image.
 
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