Multiple file upload to SQL

I am looking for an example of uploading a file(s) to an SQL server. I want to be able to upload multiple files to SQL using the OpenFileDialog to select the files. This is a windows form app not a web client. I am looking for an example in c# if possible.
Thank You
Who is Participating?
anarki_jimbelConnect With a Mentor Commented:
I'd say the key point is how to save a file in a database. If you know how to do that you may save multiple files.
How to save a file (examples):

To save multiple files I'd probubly write my own custom control to navigate to a proper folder and select files to be saved (or add them to a list). Then run a loop and save each file individually.
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
open file dialog has a multi select property which you can set as true

when multiple files have been selected then you can loop for the number of files and then convert them to byte array and then upload them to the sql server
sample to save to sql server
NoSleepForTheWearyAuthor Commented:
Thank you for the responces. I am looking for how to parse the the value returned from the OpenFileDialog.  This one is close to what I am looking for but I can seem to figure out what is the delimeter between the file names in the return string when choosing more then one.
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
the openfiledialog exposes a filenames property which is an array of files
hope that helps - ragi
NoSleepForTheWearyAuthor Commented:
This is what I came up with thanks for the input.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace WriteBlob
    public partial class Form1 : Form
        SqlConnection sqlCnnUPDATE = new System.Data.SqlClient.SqlConnection();
        public Form1()
            sqlCnnUPDATE.ConnectionString = ("data source=SQLSERVER;"+
                "initial catalog=FILEDB;"+
                "user id=sa;password=password;");
        public void UploadFile(
            string pFILENAMEDESC,
            string pFILEDESC,
            float pRUNNINGTIME,
            DateTime pFILEDATE,
            int FILETYPE,
            string FilePath)
                byte[] file = GetFile(FilePath);
                // Construct INSERT Command
                SqlCommand addEmp = new SqlCommand(
                    "INSERT INTO TBLFILES (" +
                addEmp.Parameters.Add("@FILENAMEDESC", SqlDbType.NVarChar, 100).Value = pFILENAMEDESC;
                addEmp.Parameters.Add("@FILEDESC", SqlDbType.NVarChar, 300).Value = pFILEDESC;
                addEmp.Parameters.Add("@RUNNINGTIME", SqlDbType.Float).Value = pRUNNINGTIME;
                addEmp.Parameters.Add("@FILEBLOB", SqlDbType.Image, file.Length).Value = file;
                addEmp.Parameters.Add("@FILEDATE", SqlDbType.DateTime).Value = pFILEDATE;
                addEmp.Parameters.Add("@FILETYPE", SqlDbType.Int).Value = FILETYPE;
                // Open the Connection and INSERT the BLOB into the Database
                if (sqlCnnUPDATE.State != ConnectionState.Open)
        // **** Read Image into Byte Array from Filesystem
        public static byte[] GetFile(string filePath)
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] file = br.ReadBytes((int)fs.Length);
            return file;
        private void button1_Click(object sender, EventArgs e)
            if (ofd.ShowDialog() == DialogResult.OK)
                string filename;
                string FilePath;
                foreach (string fileListItem in ofd.FileNames)
                    filename = System.IO.Path.GetFileName(fileListItem);
                    FilePath = System.IO.Path.GetDirectoryName(fileListItem) + '\\' + filename;
                    UploadFile(filename, filename, 60, DateTime.Now, 1, FilePath);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.