0% found this document useful (0 votes)
98 views

Insert, Update, Delete, Search Image in SQL - C#, JAVA, PHP, Programming, Source Code

This document discusses how to insert, update, delete, search and display images stored in a SQL database using VB.Net. It explains how to add, edit and delete images from the database, search for images by ID and display images from the database in a datagridview. The document provides links to 5 YouTube video parts that demonstrate the techniques.

Uploaded by

Benny Bav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Insert, Update, Delete, Search Image in SQL - C#, JAVA, PHP, Programming, Source Code

This document discusses how to insert, update, delete, search and display images stored in a SQL database using VB.Net. It explains how to add, edit and delete images from the database, search for images by ID and display images from the database in a datagridview. The document provides links to 5 YouTube video parts that demonstrate the techniques.

Uploaded by

Benny Bav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

VB.

Net - Insert, Update, Delete, Search Image In SQL

How To Insert Update Delete Search Display


Images In SQL Database Using VB.Net
Shares

(https://3.bp.blogspot.com/-KzRYHaNtTHc/V-pYR_fzezI/AAAAAAAAB4Y/gS-
f1J7JNIIQF34MBY0zD1PcFlpB1dzfACLcB/s1600/vb.net%2BInsert%2BUpdate%2BDelete%2BSea
rch%2BDisplay%2BImages%2BFrom%2BSQL%2B0.png)

In This VB.NET (http://1bestcsharp.blogspot.com/search/label/vb.net) Tutorial We Will


See How To :

- Add Image Into SQL Server Database


- Edit Selected Image From SQL Server Database
- Delete Selected Image From SQL Server Database
- Search Image By ID From SQL Server Database
- Show Image From SQL Server Database Into Datagridview

Using Visual Basic .NET


(http://1bestcsharp.blogspot.com/search/label/vb.net) Programming Language.
VB.Net - How To Insert Update Delete Search Display Images Fr…
Fr…

Shares
(h
tt
p:
//
sa
ve
Shares
fr
o
m
.n
et
/?
ur
l=
ht
tp
s
%
3
A
%
2
F
%
2
F
w
w
w.
y
o
ut
u
b
e.
c
o
m
%
2
F
w
at
c
Shares h
%
3
Fv
%
3
Di
0
yT
K
q
BJ
O
_
U
&
ut
m
_s
o
ur
ce
=c
hr
o
m
e
&
ut
m
_
m
e
di
di
u
m
=
e
xt
Shares e
n
si
o
n
s
&
ut
m
_c
a
m
p
ai
g
n
=l
in
k_
m
o
di
fi
er
=> Part 2 (https://www.youtube.com/watch?v=i0yTKqBJO_U) )
Shares
(h
tt
p:
//
s
a
Shares
v
ef
r
o
m
.n
et
/?
u
rl
=
h
tt
p
s
%
3
A
%
2
F
%
2
F
w
w
w
.y
o
u
t
u
b
e.
c
o
m
%
Shares 2
F
w
at
c
h
%
3
F
v
%
3
D
y
3
p
g
6-
N
Q
7
b
Q
&
u
t
m
_s
o
u
rc
e
=
c
c
h
r
o
m
e
Shares &
u
t
m
_
m
e
di
u
m
=
e
xt
e
n
si
o
n
s
&
u
t
m
_c
a
m
p
ai
g
n
=l
in
k_
m
o
di
fi
er
=> Part 3 (https://www.youtube.com/watch?v=y3pg6-NQ7bQ) )
Shares
Shares
(h
tt
p:
//
s
a
Shares
v
ef
r
o
m
.n
et
/?
u
rl
=
h
tt
p
s
%
3
A
%
2
F
%
2
F
w
w
w
.y
o
u
t
u
b
e.
c
o
m
%
Shares 2
F
w
at
c
h
%
3
F
v
%
3
D
G
Fc
gi
m
4
vI
h
U
&
u
t
m
_s
o
u
rc
e
=
c
h
r
r
o
m
e
&
u
Shares t
m
_
m
e
di
u
m
=
e
xt
e
n
si
o
n
s
&
u
t
m
_c
a
m
p
ai
g
n
=l
in
k_
m
o
di
di
fi
er
=> Part 4 (https://www.youtube.com/watch?v=GFcgim4vIhU) )

Shares
Shares
(h
tt
p:
//
s
a
Shares
v
ef
r
o
m
.n
et
/?
u
rl
=
h
tt
p
s
%
3
A
%
2
F
%
2
F
w
w
w
.y
o
u
t
u
b
e.
c
o
m
%
Shares 2
F
w
at
c
h
%
3
F
v
%
3
D
N
2
R
pJ
5
O
C
V
u
s
&
u
t
m
_s
o
u
rc
e
=
c
c
h
r
o
m
e
Shares &
u
t
m
_
m
e
di
u
m
=
e
xt
e
n
si
o
n
s
&
u
t
m
_c
a
m
p
ai
g
n
=l
in
k_
m
o
di
fi
er
=> Part 5 (https://www.youtube.com/watch?v=N2RpJ5OCVus) )
Shares
Shares
(h
tt
p:
//
s
a
Shares
v
ef
r
o
m
.n
et
/?
u
rl
=
h
tt
p
s
%
3
A
%
2
F
%
2
F
w
w
w
.y
o
u
t
u
b
e.
c
o
m
%
Shares 2
F
w
at
c
h
%
3
F
v
%
3
D
n
qi
0z
j-
c
N
8
U
&
u
t
m
_s
o
u
rc
e
=
c
h
r
r
o
m
e
&
u
Shares t
m
_
m
e
di
u
m
=
e
xt
e
n
si
o
n
s
&
u
t
m
_c
a
m
p
ai
g
n
=l
in
k_
m
o
di
di
fi
er
=> Part 6 (https://www.youtube.com/watch?v=nqi0zj-cN8U) )

Shares
Shares
(h
tt
p:
//
s
a
Shares
v
ef
r
o
m
.n
et
/?
u
rl
=
h
tt
p
s
%
3
A
%
2
F
%
2
F
w
w
w
.y
o
u
t
u
b
e.
c
o
m
%
Shares 2
F
w
at
c
h
%
3
F
v
%
3
D
Fl
q
Y
H
d
ui
M
5
g
&
u
t
m
_s
o
u
rc
e
=
c
h
h
r
o
m
e
&
Shares u
t
m
_
m
e
di
u
m
=
e
xt
e
n
si
o
n
s
&
u
t
m
_c
a
m
p
ai
g
n
=l
in
k_
m
o
di
fi
er
=> Part 7 (https://www.youtube.com/watch?v=FlqYHduiM5g) )

Shares
Shares

Project Source Code:

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class VbNet_Insert_Update_Delete_Search_Display_Images_From_SQL

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")
Private Sub
VbNet_Insert_Update_Delete_Search_Display_Images_From_SQL_Load(sender As Object,
e As EventArgs) Handles MyBase.Load

Dim command As New SqlCommand("select * from table_images", connection)


Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)

DataGridView1.AllowUserToAddRows = False

DataGridView1.RowTemplate.Height = 100
Dim imgc As New DataGridViewImageColumn
DataGridView1.DataSource = table

imgc = DataGridView1.Columns(3)
imgc.ImageLayout = DataGridViewImageCellLayout.Stretch

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim opf As New OpenFileDialog

opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"


Shares

If opf.ShowDialog = Windows.Forms.DialogResult.OK Then

PictureBox1.Image = Image.FromFile(opf.FileName)

End If

Dim ms As New MemoryStream


PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

End Sub

Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles


DataGridView1.Click

Dim img As Byte()


img = DataGridView1.CurrentRow.Cells(3).Value
Dim ms As New MemoryStream(img)
PictureBox1.Image = Image.FromStream(ms)

TextBoxID.Text = DataGridView1.CurrentRow.Cells(0).Value
TextBoxName.Text = DataGridView1.CurrentRow.Cells(1).Value
TextBoxDesc.Text = DataGridView1.CurrentRow.Cells(2).Value

End Sub

Public Sub ExecuteMyQuery(MyCommand As SqlCommand, MyMessage As String)

connection.Open()

If MyCommand.ExecuteNonQuery = 1 Then
MessageBox.Show(MyMessage)

Else

MessageBox.Show("Query Not Executed")


Shares
End If

connection.Close()

populateDatagridview("")

End Sub

Private Sub BTN_INSERT_Click(sender As Object, e As EventArgs) Handles


BTN_INSERT.Click

Dim ms As New MemoryStream


PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim img() As Byte
img = ms.ToArray()
Dim insertQuery As String = "INSERT INTO
Table_Images(name,description,the_image) VALUES('" & TextBoxName.Text & "','" &
TextBoxDesc.Text & "', @img )"

Dim command As New SqlCommand(insertQuery, connection)


command.Parameters.Add("@img", SqlDbType.Image).Value = img

ExecuteMyQuery(command, " IMage Inserted ")

End Sub

Private Sub BTN_UPDATE_Click(sender As Object, e As EventArgs) Handles


BTN_UPDATE.Click

Dim ms As New MemoryStream


PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim img() As Byte
img = ms.ToArray()
Dim updateQuery As String = "UPDATE Table_Images SET name = '" &
TextBoxName.Text & "',description = '" & TextBoxDesc.Text & "',the_image = @img
WHERE id = " & TextBoxID.Text

Dim command As New SqlCommand(updateQuery, connection)


Shares
command.Parameters.Add("@img", SqlDbType.Image).Value = img

ExecuteMyQuery(command, " IMage Updated ")

End Sub

Private Sub BTN_DELETE_Click(sender As Object, e As EventArgs) Handles


BTN_DELETE.Click

Dim deleteQuery As String = "DELETE FROM Table_Images WHERE id = " &


TextBoxID.Text

Dim command As New SqlCommand(deleteQuery, connection)

ExecuteMyQuery(command, " IMage Deleted ")

End Sub

Public Sub populateDatagridview(valueToSearch As String)

Dim searchQuery As String = "SELECT * From Table_Images WHERE


CONCAT(name,description) like '%" & valueToSearch & "%'"

Dim command As New SqlCommand(searchQuery, connection)


Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)

DataGridView1.AllowUserToAddRows = False

DataGridView1.RowTemplate.Height = 100
Dim imgc As New DataGridViewImageColumn
DataGridView1.DataSource = table

imgc = DataGridView1.Columns(3)
imgc.ImageLayout = DataGridViewImageCellLayout.Stretch

End Sub
Shares

Private Sub TextBoxSearch_TextChanged(sender As Object, e As EventArgs) Handles


TextBoxSearch.TextChanged

populateDatagridview(TextBoxSearch.Text)

End Sub

Private Sub BTN_FIND_Click(sender As Object, e As EventArgs) Handles BTN_FIND.Click

Dim command As New SqlCommand("select * from table_images WHERE id = @id ",


connection)

command.Parameters.Add("@id", SqlDbType.Int).Value = TextBoxID.Text

Dim adapter As New SqlDataAdapter(command)

Dim table As New DataTable()

adapter.Fill(table)

If table.Rows.Count() <= 0 Then

MessageBox.Show("No Data Found")

Else

TextBoxID.Text = table.Rows(0)(0).ToString()
TextBoxName.Text = table.Rows(0)(1).ToString()
TextBoxDesc.Text = table.Rows(0)(2).ToString()

Dim img() As Byte


img = table.Rows(0)(3)

Dim ms As New MemoryStream(img)

PictureBox1.Image = Image.FromStream(ms)
Shares

End If

End Sub
End Class

///////////////OUTPUT:

(https://1.bp.blogspot.com/-

dLviq4PcN1s/V-
pfTSqphLI/AAAAAAAAB4o/sitcjEU0dhkEt64Ue7Ad9_2qhuxqHAhRwCLcB/s1600/VbNet_Insert_U
pdate_Delete_Search_Display_Images_From_SQL.PNG)
if you want the source code click on the download button
below

Shares

(https://sellfy.com/1bestcsharp/p/vbnet-insert-update-delete-search-image-in-sql-database/)

disclaimer: you will get the source code without the database script.

Download Projects Source Code

▶ Get Java Projects (https://1bestcsharp.blogspot.com/p/java-


projects-source-code-bundle.html)

▶ Get C# Projects (https://1bestcsharp.blogspot.com/p/c-projects-


source-code-bundle.html)

▶ Get VB.Net Projects (https://1bestcsharp.blogspot.com/p/vbnet-


projects-source-code-bundle.html)

▶ Get Java + C# + VB.Net + Python Projects


(https://1bestcsharp.blogspot.com/p/get-all-projects-source-code-in-
one.html)

You might also like