Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Please Help >> I have windows form app, and I create form to insert data into sql database , i want to insert multiple images one button click in one table >>
How can do this???
this my code>>

C#
private void buttonX6_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
            openFileDialog1.Title = "My Image Browser";
            openFileDialog1.ValidateNames = true;
            openFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" 
                + "All files (*.*)|*.*";
          
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    fileNames.Clear();
                    listView1.Items.Clear();
                    foreach (string fileName in openFileDialog1.FileNames)
                    {
                        try
                        {
                            FileInfo fi = new FileInfo(fileName);
                            fileNames.Add(fi.FullName);
                            listView1.Items.Add(fi.Name, 0);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                        }
                    }
                }
            }


how can i save all images from listview into database in one click ??

What I have tried:

Help me, I have windows form app, and I create form to insert data into sql database , i want to insert multiple images one button click in one table >>
How can do this???
Posted
Updated 10-Dec-16 5:12am
v2

You have a good start since you already loop through the images. Now inside the loop, save each image using an INSERT statement. In pseudo code
create sql connection
create sql command for insert
for each image
   set bind variables for the command 
   execute the command

For storing images in SQL Server you have basically two options, either varbinary field or filestream. For using these fields, have a look at How to store and fetch binary data into a file stream column[^]

In general, when executing the statements you should use using blocks along with transaction and so on. For guidelines, refer to Properly executing database operations[^]
 
Share this answer
 
Comments
Member 12879178 10-Dec-16 13:13pm    
Thank you very much
Wendelius 11-Dec-16 4:59am    
You're most welcome :)
Add some code in your Button_Click event to create a SQL Insert command to send the data to your database. Since we have no idea where this data is held, what form it is in, or the structure of your database, that's about the best suggestion I can come up with.
 
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