Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tryed this code for saving multiple images but, In database images stored in multiple rows with same ID. EXample, three images selected, then three time rows Generated with same ID 1.

how to stores that three different images in single row with multiple columns and one single ID?

What I have tried:

C#
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
{
    string filename = Path.GetFileName(postedFile.FileName);
    string contentType = postedFile.ContentType;
    using (Stream fs = postedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string bitString = BitConverter.ToString(bytes).Length.ToString();  

            string constr = ConfigurationManager.ConnectionStrings["cstring"].ConnectionString;
            using (OleDbConnection con = new OleDbConnection(constr))
            {
                string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
                using (OleDbCommand cmd = new OleDbCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename)+
                    cmd.Parameters.AddWithValue("@ContentType", contentType)+
                    cmd.Parameters.AddWithValue("@Data", bitString);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
}

Response.Redirect(Request.Url.AbsoluteUri);
Posted
Updated 4-Feb-20 2:30am
v3
Comments
Richard Deeming 4-Feb-20 8:22am    
That depends on the structure of your database table - you would need to have three image columns in the table in order to store three images on a single row.

But that's almost certainly the wrong thing to do.

1 solution

To insert multiple images - or any other "multiple data" - on the same row of a DB, you issue one single INSERT statement providing all data items, and store each in a separate predefined column. You can't store "multiple objects" in a single column of the same row (or at least, you can't without your DB becoming a PITA to use).

And I'm guessing that isn't what you want in practice.
What I'd do is have two tables:
tblFiles with your existing ID, name, and content type.
tblImages with a separate ID, a DateTime stamp, the image data, and a Foreign Key to the ID in tblFiles.

That way, you can retrieve all the images for a Name with a simple JOIN:
SQL
SELECT f.Name, i.Data, i.TimeStamp FROM tblFiles f
JOIN tblImages i ON i.FileID = f.ID
WHERE f.Name LIKE '%Christmas Party%'
And each image will be returned as a single row.
 
Share this answer
 
Comments
Member 11774405 5-Feb-20 4:21am    
Okay, Now I will try this way and let's see what happens. Thank you.

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