vb.net 在 ms access 数据库中保存/插入图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33763773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:34:10  来源:igfitidea点击:

Save/insert image in ms access database

vb.net

提问by jane

I have a picturebox and two buttons (browse and save image). Once the user click the browse button the user will browse picture from the computer then save it to database by clicking the save button.

我有一个图片框和两个按钮(浏览和保存图像)。一旦用户单击浏览按钮,用户将从计算机上浏览图片,然后通过单击保存按钮将其保存到数据库中。

Do u have any idea on how to do it?

你知道怎么做吗?

回答by Mihail Panagiotopoulos

Here is the code, hope it helps:

这是代码,希望它有帮助:

Imports System.Data.OleDb
Public Class Form1
    Dim cn As New OleDbConnection("Provider = Microsoft.Jet.OleDb.4.0;Data Source = " & Application.StartupPath & "\database.mdb;")
    Dim cm As New OleDbCommand

    Dim bytImage() As Byte
    Private Sub Browse_Click(sender As Object, e As EventArgs) Handles Browse.Click
        Dim dialog As New OpenFileDialog()
        dialog.Title = "Browse Picture"
        dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"
        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(dialog.FileName)
        End If
    End Sub

    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Try
            Dim ms As New System.IO.MemoryStream
            Dim bmpImage As New Bitmap(PictureBox1.Image)

            bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            bytImage = ms.ToArray()
            ms.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        cn.Open()
        cm.Connection = cn
        cm.CommandType = CommandType.Text
        cm.CommandText = "INSERT INTO `table` (pic) VALUES (@image)"
        cm.Parameters.AddWithValue("@image", bytImage)
        cm.ExecuteNonQuery()
        cn.Close()
    End Sub
End Class

回答by user6473235

''---- Save Data In msaccess New accdb msaccess 2007 & Above Imports System.Data.OleDb Public Class Form2 Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\acordv.accdb;") Dim cm As New OleDbCommand

''----在msaccess中保存数据 New accdb msaccess 2007 & Above Imports System.Data.OleDb Public Class Form2 Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="&Application.StartupPath & "\acordv.accdb;") Dim cm As New OleDbCommand

Dim bytImage() As Byte


Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
    Dim dialog As New OpenFileDialog()
    dialog.Title = "Browse Picture"
    dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"
    If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
        PictureBox1.Image = Image.FromFile(dialog.FileName)
    End If
End Sub

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
    Try
        Dim ms As New System.IO.MemoryStream
        Dim bmpImage As New Bitmap(PictureBox1.Image)

        bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        bytImage = ms.ToArray()
        ms.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    cn.Open()
    cm.Connection = cn
    cm.CommandType = CommandType.Text
    cm.CommandText = "INSERT INTO `pic1` (pic) VALUES (@image)"
    cm.Parameters.AddWithValue("@image", bytImage)
    cm.ExecuteNonQuery()
    cn.Close()
    MsgBox("Image Saved.")
End Sub