Multiple file upload to SQL

Posted on 2008-11-03
Last Modified: 2013-12-17
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
Question by:NoSleepForTheWeary
    LVL 29

    Accepted Solution

    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.
    LVL 26

    Assisted Solution

    by:Anurag Thakur
    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

    Author Comment

    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.
    LVL 26

    Assisted Solution

    by:Anurag Thakur
    the openfiledialog exposes a filenames property which is an array of files
    hope that helps - ragi

    Author Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now