0

I want to store images into sql using c# application. So I have this code, and multiple selection is true. I know that I need to use an array filenames. Can you help me to realize it? I just need to select more than one file in folder and store thme in sql data base.

browse images:

 private void btnBrowseImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
            openFileDialog1.Title = "Sekili sec";

            openFileDialog1.InitialDirectory = "E:\\IKA";
            openFileDialog1.Filter = "All files (*.jpg)|*.jpg";
           // openFileDialog1.FilterIndex = 10;
            openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    txtImagePath.Text = openFileDialog1.FileName; 
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

file to binary

byte[] ReadImageToBytes(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, 
                                                    FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            data = br.ReadBytes((int)numBytes);
            return data;
        }

saving them in sql:

private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Read Image Bytes into a byte array
                byte[] imageSampleData = ReadImageToBytes(txtImagePath.Text);

                //Initialize SQL Server Connection
             //   SqlConnection con = new SqlConnection(txtConnectionString.Text);

                String strConnString = "Data Source=1.1.1.1;Initial Catalog=Test1;User Id=dffr;Password=dffr;";
                SqlConnection con = new SqlConnection(strConnString);
                //Set insert query
                string query = "INSERT INTO tblImages (FullPath,MyImageSample) values(@FullPath, @MyImageSample)";

                //Initialize SqlCommand object for insert.
                SqlCommand cmd = new SqlCommand(query, con);

                cmd.Parameters.Add(new SqlParameter("@FullPath",
                                            (object)txtImagePath.Text));

                cmd.Parameters.Add(new SqlParameter("@MyImageSample",
                                                    (object)imageSampleData));

                //Open connection and execute insert query.
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


            }
            catch 
            {
                MessageBox.Show("Error while saving image.", "Error");
            }
        }

1 Answer 1

2

You need to use FileNames property to get the list of selected files.

 foreach(string filename in openFileDialog1.FileNames)
 {
    //1. read file 
    //2. store content of file into database.
 }
Sign up to request clarification or add additional context in comments.

2 Comments

what should be in the body of a loop?
it give me a mistake "'openFileDialog1' does not exist in the current context".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.