NoSleepForTheWeary
asked on
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
Thank You
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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);
}
}
}
}
}
ASKER