【问题标题】:how to insert image in mysql database using vb.net and adodb connection如何使用 vb.net 和 adodb 连接在 mysql 数据库中插入图像
【发布时间】:2015-03-15 17:14:01
【问题描述】:

我想在 vb.net 2008 中使用 adodb 连接将图像插入 mysql 数据库。

我正在使用选择查询将数据插入数据库,这是我添加或保存数据的代码...

    rs.Open("select * from registration where Debt_ID = '" & txtDebt_ID.Text & "' ", cnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockPessimistic)
    If rs.RecordCount = 1 Then
        MsgBox("ID already exist!", MsgBoxStyle.Exclamation, vbOK)
        rs.Close()
        cnn.Close() 

        Exit Sub
    Else

        rs.AddNew()
        rs.Fields("Debt_ID").Value = txtDebt_ID.Text
        rs.Fields("LastName").Value = txt_Lastname.Text
        rs.Fields("firstName").Value = txt_Firstname.Text
        rs.Fields("MiddleName").Value = txt_Middlename.Text
        rs.Fields("age").Value = txt_Age.Text
        rs.Fields("birthdate").Value = txt_Birthdate.Text
        rs.Fields("civil_status").Value = txtCivil_status.Text
        rs.Fields("address").Value = txt_Address.Text
        rs.Fields("occupation").Value = txt_Address.Text
        rs.Fields("contact_no").Value = txt_Contact.Text
        'rs.Fields("picture").Value = PictureBox1.Image
        rs.Save()
        rs.Close()
    End If

我想将数据库上的图像添加到字段图片中,并且我使用 blob 作为我的数据类型,我还想从数据库中检索图像并将其显示在图片框中...有人可以吗关于我的问题的帮助。

提前谢谢...

【问题讨论】:

  • 保存图片路径或使用BLOBdatatype
  • 我的第一个问题是你为什么要在 VB.NET 中使用 ADODB?如果您打算使用 VB6 数据访问技术,那为什么还要使用 VB.NET?如果您使用的是 .NET,那么您应该使用 .NET,这意味着使用 ADO.NET 进行数据访问。

标签: mysql vb.net


【解决方案1】:

无论您使用什么数据访问技术或数据库,您都需要先将 Image 转换为 Byte 然后保存。在检索时,您将Byte 数组转换回Image

保存:

Dim connection As New SqlConnection("connection string here")
Dim command As New SqlCommand("UPDATE MyTable SET Picture = @Picture WHERE ID = 1", connection)

'Create an Image object.'
Using picture As Image = Image.FromFile("file path here")
    'Create an empty stream in memory.'
    Using stream As New IO.MemoryStream
        'Fill the stream with the binary data from the Image.'
        picture.Save(stream, Imaging.ImageFormat.Jpeg)

        'Get an array of Bytes from the stream and assign to the parameter.'
        command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
    End Using
End Using

connection.Open()
command.ExecuteNonQuery()
connection.Close()

检索:

Dim connection As New SqlConnection("connection string here")
Dim command As New SqlCommand("SELECT Picture FROM MyTable WHERE ID = 1", connection)

connection.Open()

Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())

connection.Close()

Dim picture As Image = Nothing

'Create a stream in memory containing the bytes that comprise the image.'
Using stream As New IO.MemoryStream(pictureData)
    'Read the stream and create an Image object from the data.'
    picture = Image.FromStream(stream)
End Using

该示例适用于 ADO.NET 和 SQL Server,但无论如何使用 MemoryStream 进行转换的原理都是相同的。

【讨论】:

    【解决方案2】:

    创建一个带有BLOB字段的表,如下所示

    CREATE TABLE picture (
    ID   INTEGER AUTO_INCREMENT,
    IMAGE   BLOB, 
    PRIMARY KEY (ID)
    );
    

    使用以下查询字符串插入此表:

    Dim mstream As New System.IO.MemoryStream()
    pic_box_save.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim arrImage() As Byte = mstream.GetBuffer()
    mstream.Close()
    Try
     sql = "INSERT INTO image_in_db(id, image_data) VALUES(@image_id, @image_data)"
     sql_command = New MySqlClient.MySqlCommand(sql, sql_connection)
     sql_command.Parameters.AddWithValue("@image_id", Nothing)
     sql_command.Parameters.AddWithValue("@image_data", arrImage)
     sql_command.ExecuteNonQuery()
    Catch ex As Exception
     MsgBox(ex.Message)
     Exit Sub
    End Try
    MsgBox("Image has been saved.")
    

    【讨论】:

    • 没有。这是一个糟糕的代码,因为它不使用参数,并且只会保存String,而不是ImageString 可能包含图像文件的路径,但这并不能使其成为图像本身。有些人可能更喜欢在数据库中存储文件路径而不是图像,但这确实存在文件被移动、重命名或删除以及数据库包含不正确数据的风险。
    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2015-12-13
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多