mysql - 如何使用vb.net在mysql中存储二进制数据?

标签 mysql vb.net

我正在构建一个简单的程序,帮助人们浏览图像并将其保存在 mysql 数据库中。在 vb.net 中保存图片时遇到很大麻烦,非常感谢您的帮助...

Imports System.IO
Public Class frmSalesManager

Private Sub frmSalesManager_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
    OpenFileDialog1.Filter = "image file (*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png| all files (*.*) | *.* "
    If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
    Dim breader As New BinaryReader(fsreader)
    Dim imgbuffer(fsreader.Length) As Byte
    breader.Read(imgbuffer, 0, fsreader.Length)
    fsreader.Close()
    strsql = "Insert into Test (name, picture) VALUES (@name, @picture)"
    acsmd.CommandText = strsql

    acsmd.Connection = con
    acsmd.Parameters.AddWithValue("@name", txtname.Text)
    acsmd.Parameters.AddWithValue("@picture", imgbuffer)
    'acsmd.ExecuteNonQuery()
    acsmd.Dispose()
    MsgBox("Saved")


End Sub

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    If OpenFileDialog1.FileName <> Nothing Or OpenFileDialog1.FileName <> "" Then
        txtname.Text = OpenFileDialog1.FileName.Substring( _
OpenFileDialog1.FileName.LastIndexOf("\") + 1, _
(OpenFileDialog1.FileName.IndexOf(".", 0) - (OpenFileDialog1.FileName.LastIndexOf("\") + 1)))
        End If
    End Sub
End Class

最佳答案

我希望 con 是一个开放连接...尝试为 Button1_Click 使用此代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim imgbuffer() As Byte
        Using fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
            ReDim imgbuffer(fsreader.Length - 1)
            Using breader As New BinaryReader(fsreader)
                breader.Read(imgbuffer, 0, fsreader.Length)
            End Using
        End Using

        strsql = "Insert into Test (name, picture) VALUES (@name, @picture)"
        acsmd.CommandText = strsql

        acsmd.Connection = con
        acsmd.Parameters.AddWithValue("@name", txtname.Text)
        acsmd.Parameters.AddWithValue("@picture", imgbuffer)
        acsmd.ExecuteNonQuery()
        acsmd.Dispose()
        MsgBox("Saved")
    End Sub

关于mysql - 如何使用vb.net在mysql中存储二进制数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559532/

相关文章:

vb.net - 后台 worker 和 SaveDialog

php - 使用 switch case 以数组作为参数的 PHP 函数更新 MySQL 数据库

python - 我不知道通常使用python aiomysql。运行时间(当 aiomysql 不使用时)与 aiomysql 使用时的运行时间相同

asp.net - 如何在我的网站中显示网站部分

javascript - 将 jtable 导出到 IE 上的 javascript 中的 excel

python - VB dll 在带有 ctypes 的 python 中不起作用(函数 * 未找到)

php - MySql Group By 和 Sum 列的总值,以最小值显示

c# - LINQ 中的日期差异逻辑

python - 在 python 中使用 MySQLdb 插入 mysql 后,如何安全有效地获取行 ID?

.net - 显示引号 "