Hi smile,
Refer below sample.
Database
I have made use of the following table named tblFiles whose schema is defined as follows.
You can download the database table SQL by clicking the download link below.
Download SQL file
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGridView();
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
this.dataGridView1.Columns.Insert(0, checkBoxColumn);
}
protected void BindDataGridView()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT ID,Name FROM tblFiles", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string fileNames in openFileDialog.FileNames)
{
string contentType = "";
switch (Path.GetExtension(fileNames).ToLower())
{
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".bmp":
contentType = "image/bmp";
break;
case ".doc":
contentType = "application/msword";
break;
case ".docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case ".xls":
contentType = "application/vnd.ms-excel";
break;
case ".xlsx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case ".pdf":
contentType = "application/pdf";
break;
case ".ppt":
contentType = "application/vnd.ms-powerpoint";
break;
case ".pptx":
contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
break;
}
byte[] bytes = File.ReadAllBytes(fileNames);
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblFiles VALUES (@Name,@ContentType,@Data)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(fileNames));
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
this.BindDataGridView();
}
private void btnDownload_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells["checkBoxColumn"].Value))
{
string id = row.Cells[1].Value.ToString();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name,Data FROM tblFiles WHERE ID = @Id", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.Read())
{
byte[] bytes = (byte[])sdr["Data"];
string fileName = sdr["Name"].ToString();
string path = Path.Combine(folderBrowserDialog.SelectedPath, fileName);
File.WriteAllBytes(path, bytes);
}
}
con.Close();
}
}
}
}
MessageBox.Show("File downloaded in folder " + folderBrowserDialog.SelectedPath);
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
Me.BindDataGridView()
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
Me.dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Protected Sub BindDataGridView()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT ID,Name FROM tblFiles", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using openFileDialog As OpenFileDialog = New OpenFileDialog()
openFileDialog.Multiselect = True
If openFileDialog.ShowDialog() = DialogResult.OK Then
For Each fileNames As String In openFileDialog.FileNames
Dim contentType As String = ""
Select Case Path.GetExtension(fileNames).ToLower()
Case ".jpg"
contentType = "image/jpeg"
Case ".png"
contentType = "image/png"
Case ".gif"
contentType = "image/gif"
Case ".bmp"
contentType = "image/bmp"
Case ".doc"
contentType = "application/msword"
Case ".docx"
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Case ".xls"
contentType = "application/vnd.ms-excel"
Case ".xlsx"
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Case ".pdf"
contentType = "application/pdf"
Case ".ppt"
contentType = "application/vnd.ms-powerpoint"
Case ".pptx"
contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
End Select
Dim bytes As Byte() = File.ReadAllBytes(fileNames)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblFiles VALUES (@Name,@ContentType,@Data)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(fileNames))
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", bytes)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Next
End If
End Using
Me.BindDataGridView()
End Sub
Private Sub btnDownload_Click(sender As System.Object, e As System.EventArgs) Handles btnDownload.Click
Using folderBrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
For Each row As DataGridViewRow In dataGridView1.Rows
If Convert.ToBoolean(row.Cells("checkBoxColumn").Value) Then
Dim id As String = row.Cells(1).Value.ToString()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT Name,Data FROM tblFiles WHERE ID = @Id", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
Dim bytes As Byte() = CType(sdr("Data"), Byte())
Dim fileName As String = sdr("Name").ToString()
Dim path As String = System.IO.Path.Combine(folderBrowserDialog.SelectedPath, fileName)
File.WriteAllBytes(path, bytes)
End If
End Using
con.Close()
End Using
End Using
End If
Next
End If
MessageBox.Show("File downloaded in folder " + folderBrowserDialog.SelectedPath)
End Using
End Sub
Screenshots
Record In Database
For list of MIME types refer below link.
Complete list of MIME types