Hi smile,
Refer below sample.
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Namespaces
C#
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Data.SqlClient
Imports System.IO
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
BindGrid();
DataGridViewLinkColumn lnkDownload = new DataGridViewLinkColumn();
lnkDownload.UseColumnTextForLinkValue = true;
lnkDownload.LinkBehavior = LinkBehavior.SystemDefault;
lnkDownload.Name = "lnkDownload";
lnkDownload.HeaderText = "Download";
lnkDownload.Text = "Download";
dataGridView1.Columns.Insert(2, lnkDownload);
dataGridView1.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick);
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
int id = Convert.ToInt16((row.Cells[0].Value));
byte[] bytes;
string fileName, contentType;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
Stream stream;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = fileName;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
stream = saveFileDialog.OpenFile();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
}
}
con.Close();
}
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select Id, Name from tblFiles", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
private void UploadFile(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
byte[] bytes = File.ReadAllBytes(fileName);
string contentType = "";
//Set the contenttype based on File Extension
switch (Path.GetExtension(fileName))
{
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".bmp":
contentType = "image/bmp";
break;
}
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(fileName));
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
this.BindGrid();
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
BindGrid()
Dim lnkDownload As DataGridViewLinkColumn = New DataGridViewLinkColumn()
lnkDownload.UseColumnTextForLinkValue = True
lnkDownload.LinkBehavior = LinkBehavior.SystemDefault
lnkDownload.Name = "lnkDownload"
lnkDownload.HeaderText = "Download"
lnkDownload.Text = "Download"
dataGridView1.Columns.Insert(2, lnkDownload)
AddHandler dataGridView1.CellContentClick, AddressOf DataGridView_CellClick
End Sub
Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
Dim id As Integer = Convert.ToInt16((row.Cells(0).Value))
Dim bytes As Byte()
Dim fileName, contentType As String
Using con As SqlConnection = New SqlConnection(ConnectionString)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id"
cmd.Parameters.AddWithValue("@Id", id)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
bytes = CType(sdr("Data"), Byte())
contentType = sdr("ContentType").ToString()
fileName = sdr("Name").ToString()
Dim stream As Stream
Dim saveFileDialog As SaveFileDialog = New SaveFileDialog()
saveFileDialog.Filter = "All files (*.*)|*.*"
saveFileDialog.FilterIndex = 1
saveFileDialog.RestoreDirectory = True
saveFileDialog.FileName = fileName
If saveFileDialog.ShowDialog() = DialogResult.OK Then
stream = saveFileDialog.OpenFile()
stream.Write(bytes, 0, bytes.Length)
stream.Close()
End
End If
End Using
con.Close()
End Using
End Using
End If
End Sub
Private Sub BindGrid()
Using con As SqlConnection = New SqlConnection(ConnectionString)
Using cmd As SqlCommand = New SqlCommand("select Id, Name from tblFiles", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
Private Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs)
Using openFileDialog1 As OpenFileDialog = New OpenFileDialog()
If openFileDialog1.ShowDialog() = DialogResult.OK Then
Dim fileName As String = openFileDialog1.FileName
Dim bytes As Byte() = File.ReadAllBytes(fileName)
Dim contentType As String = ""
Select Case Path.GetExtension(fileName)
Case ".jpg"
contentType = "image/jpeg"
Case ".png"
contentType = "image/png"
Case ".gif"
contentType = "image/gif"
Case ".bmp"
contentType = "image/bmp"
End Select
Using conn As SqlConnection = New SqlConnection(ConnectionString)
Dim sql As String = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(fileName))
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", bytes)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
Me.BindGrid()
End If
End Using
End Sub
Private Class CSharpImpl
<Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class
Screenshot