Saturday, January 19, 2013

VarBinary Picture in VB.Net

Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Drawing.Image

Public Class Form1
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim ds As New DataSet
    Dim dr As SqlDataReader
    Dim dt As DataTable
    Dim dt1 As DataTable
    Dim sql As String
    Dim icount As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            'Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
            'Dim mbr As BinaryReader = New BinaryReader(st)
            'Dim buffer(st.Length) As Byte
            'mbr.Read(buffer, 0, CInt(st.Length))
            'st.Close()
            con = New SqlConnection("Data Source=DUMMYSERVER;Initial Catalog=PERDB;Persist Security Info=True;User ID=SA;Password=SA")
            con.Open()
            sql = "insert into pic values ('" & TextBox1.Text & "', @PhotoID)"
            cmd = New SqlCommand(sql, con)
            'cmd.Parameters.Add("@PhotoID", System.Data.SqlDbType.Binary, buffer.Length).Value = buffer
            cmd.Parameters.Add(New SqlClient.SqlParameter("@PhotoID", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog1.FileName)
            cmd.ExecuteNonQuery()
            MsgBox("Save")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
        If Not PictureBox1.Image Is Nothing Then
            PictureBox1.Image.Dispose()
            PictureBox1.Image = Nothing
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim dt As New DataTable
            con = New SqlConnection("Data Source=DUMMYSERVER;Initial Catalog=PERDB;Persist Security Info=True;User ID=SA;Password=SA")
            con.Open()

            sql = "SELECT * FROM PIC where id = '" & TextBox1.Text & "'"
            da = New System.Data.SqlClient.SqlDataAdapter(sql, con)
            da.Fill(dt)
            Dim b As Byte() = New Byte(-1) {}
            b = DirectCast(dt.Rows.Item(0).Item("photo"), [Byte]())
            Dim ms As New MemoryStream(b)
            PictureBox1.Image = Image.FromStream(ms)
            dt.Dispose()
        Catch es As Exception
            MsgBox(es.Message)
        End Try
        con.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            con = New SqlConnection("Data Source=DUMMYSERVER;Initial Catalog=PERDB;Persist Security Info=True;User ID=SA;Password=SA")
            con.Open()
            sql = "Delete from pic where id = '" & TextBox1.Text & "'"
            cmd = New SqlCommand(sql, con)
            icount = cmd.ExecuteNonQuery
            MsgBox("Delete")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            OpenFileDialog1.Filter = "Jpg Files(*.jpg)|*.jpg|Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif"
            OpenFileDialog1.ShowDialog()
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        Catch ex As Exception
            MsgBox("No Image Selected")
            'MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And (e.KeyChar <> Chr(8)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Select()
    End Sub
End Class

No comments:

Post a Comment