Hi ahmedsa,
Refer below sample.
Namespaces
C#
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Data.SqlClient
Imports System.IO
Code
C#
string constr = @"Data Source=.;Initial Catalog=dbFiles;uid=sa;pwd=pass@123";
public byte[] bytesImage { get; set; }
public int Id { get; set; }
private void timer1_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString();
using (SqlConnection conn = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("SELECT Data FROM tblFiles WHERE Id = @Id", conn);
cmd.Parameters.AddWithValue("@Id", Id);
conn.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
if (BitConverter.ToInt64(bytesImage, 0) == BitConverter.ToInt64(bytesImage, 0))
{
pictureBox1.Image = null;
}
else
{
pictureBox1.Image = Image.FromStream(new MemoryStream(bytes));
}
conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
lblTime.Text = DateTime.Now.ToString();
SqlCommand cmd = null;
using (SqlConnection conn = new SqlConnection(constr))
{
cmd = new SqlCommand("SELECT MAX(ID) FROM tblFiles", conn);
conn.Open();
int id = Convert.ToInt32(cmd.ExecuteScalar());
cmd = new SqlCommand("SELECT Data FROM tblFiles WHERE Id = @Id", conn);
cmd.Parameters.AddWithValue("@Id", id);
byte[] bytes = (byte[])cmd.ExecuteScalar();
conn.Close();
pictureBox1.Image = Image.FromStream(new MemoryStream(bytes));
bytesImage = bytes;
Id = id;
}
}
VB.Net
Private constr As String = "Data Source=.;Initial Catalog=dbFiles;uid=sa;pwd=pass@123"
Public Property bytesImage As Byte()
Public Property Id As Integer
Private Sub timer1_Tick(sender As System.Object, e As System.EventArgs) Handles timer1.Tick
lblTime.Text = DateTime.Now.ToString()
Using conn As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT Data FROM tblFiles WHERE Id = @Id", conn)
cmd.Parameters.AddWithValue("@Id", Id)
conn.Open()
Dim bytes As Byte() = CType(cmd.ExecuteScalar(), Byte())
If BitConverter.ToInt64(bytesImage, 0) = BitConverter.ToInt64(bytesImage, 0) Then
pictureBox1.Image = Nothing
Else
pictureBox1.Image = Image.FromStream(New MemoryStream(bytes))
End If
conn.Close()
End Using
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
timer1.Enabled = True
lblTime.Text = DateTime.Now.ToString()
Dim cmd As SqlCommand = Nothing
Using conn As SqlConnection = New SqlConnection(constr)
cmd = New SqlCommand("SELECT MAX(ID) FROM tblFiles", conn)
conn.Open()
Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
cmd = New SqlCommand("SELECT Data FROM tblFiles WHERE Id = @Id", conn)
cmd.Parameters.AddWithValue("@Id", id)
Dim bytes As Byte() = CType(cmd.ExecuteScalar(), Byte())
conn.Close()
pictureBox1.Image = Image.FromStream(New MemoryStream(bytes))
bytesImage = bytes
id = id
End Using
End Sub
Screenshot