Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:52:52+00:00 2026-05-30T14:52:52+00:00

I want to store images into sql using c# application. So I have this

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:52:53+00:00Added an answer on May 30, 2026 at 2:52 pm

    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.
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using Ruby on Rails and i want to store images into the cassandra
From my application, I want to store some images into my SD card. For
I'm currently writing an application that allows one to store images, and then tag
I have multiple images stored at URLs like: /uploads/hash/IMAGE001.jpg . Using jQuery UI's sortable()
I have the number of images from the gallary. I want to store the
Hai! I am new to android. I want to store the images into the
Hi i want to store images into database.I created table with fields like imgID
I want to store an image(from url) into a sqlite database. For that I
I'm trying to store three images from a form (PictureBox's) into a SQL Database
I have about 60 images I want to store in Core Data, 30 of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.