• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 815
  • Last Modified:

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
0
NoSleepForTheWeary
Asked:
NoSleepForTheWeary
  • 2
  • 2
3 Solutions
 
anarki_jimbelCommented:
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
 
Anurag ThakurCommented:
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 ThakurCommented:
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now