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
NoSleepForTheWearyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
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):
http://www.akadia.com/services/dotnet_read_write_blob.html
http://www.daniweb.com/forums/thread126860.html

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anurag ThakurTechnical ManagerCommented:
open file dialog has a multi select property which you can set as true
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.multiselect(VS.80).aspx

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
http://www.shabdar.org/store-save-images-in-sql-server.html
0
NoSleepForTheWearyAuthor Commented:
Thank you for the responces. I am looking for how to parse the the value returned from the OpenFileDialog.  http://www.shabdar.org/store-save-images-in-sql-server.html  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.
0
Anurag ThakurTechnical ManagerCommented:
the openfiledialog exposes a filenames property which is an array of files
openFileDialog1.FileNames[0].ToString();
hope that helps - ragi
0
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()
        {
            InitializeComponent();
            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)
        {
            try
            {
 
              
                byte[] file = GetFile(FilePath);
 
                // Construct INSERT Command
                SqlCommand addEmp = new SqlCommand(
                    "INSERT INTO TBLFILES (" +
                    "FILENAMEDESC,FILEDESC,RUNNINGTIME,FILEBLOB,FILEDATE,FILETYPE) " +
                    "VALUES(@FILENAMEDESC,@FILEDESC,@RUNNINGTIME,@FILEBLOB,@FILEDATE,@FILETYPE)", sqlCnnUPDATE);
 
                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)
                {
                    sqlCnnUPDATE.Open();
                } 
                addEmp.ExecuteNonQuery();
                sqlCnnUPDATE.Close();
 
                
            }
            catch
            {
                
            }
 
 
 
 
        }
        
 
        // **** 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);
 
            br.Close();
            fs.Close();
 
            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;
                    listBox1.Items.Add((object)filename);
                    UploadFile(filename, filename, 60, DateTime.Now, 1, FilePath);
 
                }
 
            }
 
 
        }
 
     
        
    }
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.