Full Code (Part1+part2)
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging
Public Class Form2
Dim con1 As New SqlConnection("Server=.;Database=images;integrated security=true")
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bind1()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim openfiledialog1 As New OpenFileDialog
openfiledialog1.Filter = "images|*.jpg;*.png;*.gif;*.bmp"
If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(openfiledialog1.FileName)
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim command1 As New SqlCommand("Insert into table1(name,age,img1)values(@name,@age,@img)", con1)
command1.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text
command1.Parameters.Add("@age", SqlDbType.VarChar).Value = TextBox2.Text
Dim memstr As New MemoryStream
PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)
command1.Parameters.Add("@img", SqlDbType.Image).Value = memstr.ToArray
If con1.State = ConnectionState.Closed Then
con1.Open()
End If
command1.ExecuteNonQuery()
MessageBox.Show("image inserted")
con1.Close()
bind1()
End Sub
Private Sub bind1()
Dim command1 As New SqlCommand("Select * from table1 order by id desc", con1)
Dim adapter As New SqlDataAdapter(command1)
Dim table As New DataTable()
adapter.Fill(table)
DataGridView1.AllowUserToAddRows = False
DataGridView1.RowTemplate.Height = 90
Dim pic1 As New DataGridViewImageColumn
DataGridView1.DataSource = table
pic1 = DataGridView1.Columns(3)
pic1.ImageLayout = DataGridViewImageCellLayout.Stretch
End Sub
Private Sub DataGridView1_Click(sender As Object, e As System.EventArgs) Handles DataGridView1.Click
Label4.Text = DataGridView1.CurrentRow.Cells(0).Value
TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value
TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value
Dim img As Byte()
img = DataGridView1.CurrentRow.Cells(3).Value
Dim memstr As New MemoryStream(img)
PictureBox1.Image = Image.FromStream(memstr)
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim memstr As New MemoryStream
PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)
Dim img() As Byte
img = memstr.ToArray()
Dim update1 As String = "Update table1 set name='" & TextBox1.Text & "',age='" & TextBox2.Text & "',img1=@img where id=" & Label4.Text
Dim command1 As New SqlCommand(update1, con1)
command1.Parameters.Add("@img", SqlDbType.Image).Value = img
If con1.State = ConnectionState.Closed Then
con1.Open()
End If
command1.ExecuteNonQuery()
MessageBox.Show("Image updated")
con1.Close()
bind1()
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Dim delete1 As String = "Delete from table1 where id=" & Label4.Text
Dim command1 As New SqlCommand(delete1, con1)
If con1.State = ConnectionState.Closed Then
con1.Open()
End If
command1.ExecuteNonQuery()
MessageBox.Show("Image deleted")
con1.Close()
bind1()
End Sub
End Class
No comments:
Post a Comment