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


How can I insert multiple images into database with one asp.net button ?

Please guide me with sample codes or links.

Thanks
Jaheena
Posted
Updated 5-Apr-11 20:26pm
v2

1 solution

How you do this will depend on how you store your images: if you always have five images per row in the database, then it is one command. Otherwise you will need to issue separate row INSERT commands.
Assuming it is one image per row:

1) Bring up SQL Server Management Studio.
2) Connect, and open the database you want to add the image to.
3) Expand the tables, and either add a new table with an appropriate index field, or right click teh table and select design.
4) Add a field called "myImage", and make its datatype "image". Allow nulls.
5) Close SQL Server Management studio.
To add images to the table (I will assume a new record, the table has an Identity field, and just teh myImage column):
C#
using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myImage) VALUES (@IM)", con))
        {
        byte[] imageData = File.ReadAllBytes(@"F:\Temp\Picture1.jpg");
        com.Parameters.AddWithValue("@IM", imageData);
        com.ExecuteNonQuery();
        imageData = File.ReadAllBytes(@"F:\Temp\Picture2.jpg");
        com.Parameters["@IM"].Value = imageData;
        com.ExecuteNonQuery();
        }
    }
If it is multiple images per row, then just add a second image field, and include it in the INSERT statement.
 
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