• 0

[VB.NET] SQL Server-MS Access


Question

Hello,

I have code in VB.NET manner that reads and saves bitmaps from/within SQL Server but i would like to make it workable with Access DB file as well.... as you know Access doesn't have binary (image) format to keeps the pictures inside ... by the way the code and DB i use with SQL Server keeps the bitmaps in image format and if you try to see data in table you will see only <Binary> ... lol

Here is my code and all that I?m trying is to read/save pictures with an Access DB file instead SQL Server:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With OpenFileDialog1
            .InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            .FilterIndex = 2
        End With
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            With PictureBox1
                .Image = Image.FromFile(Me.OpenFileDialog1.FileName)
                .SizeMode = PictureBoxSizeMode.CenterImage
            End With
        End If
        Me.Label1.Text = Me.OpenFileDialog1.FileName.ToString
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ms As New MemoryStream
        Me.PictureBox1.Image.Save(ms, Me.PictureBox1.Image.RawFormat)
        Dim arrayImage() As Byte = ms.GetBuffer
        ms.Close() ' Closes the Memory Stream
        Dim nStr As String = Me.Label1.Text.Substring(Me.Label1.Text.LastIndexOf("\") + 1)
        Dim strQuery As String = "INSERT INTO Pic(Name, Picture) VALUES(@Name, @Picture)"
        Dim objcommand As New SqlCommand(strQuery, Me.SqlConnection1)
        With objcommand
            .Parameters.Add(New SqlParameter("@Name", SqlDbType.NVarChar, 50)).Value = nStr
            .Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrayImage
        End With
        Me.SqlConnection1.Open()
        objcommand.ExecuteNonQuery()
        MessageBox.Show("Image Saved Into the DataBase")
        Me.SqlConnection1.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.SqlConnection1.Open()
        Me.SqlDataAdapter1.Fill(Me.DataSet11.Pic)
        With Me.ListBox1
            .DataSource = Me.DataSet11.Pic
            .DisplayMember = "name"
        End With
        Me.SqlConnection1.Close()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim arrayImage() As Byte = CType(Me.DataSet11.Tables(0).Rows(Me.ListBox1.SelectedIndex)("Picture"), Byte())
        Dim ms As New MemoryStream(arrayImage)
        With Me.PictureBox1
            .Image = Image.FromStream(ms)
            .SizeMode = PictureBoxSizeMode.CenterImage
        End With
    End Sub

I would be very thankful if someone finds time to help me ... thanks in advance

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.