将图像保存到mysql数据库中 [英] saving images into mysql database

查看:62
本文介绍了将图像保存到mysql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我希望有人能帮助我,我希望将图像存储到Blob字段上的mysql数据库中

hello, i hope someone can help me i'm tring to store images in to a mysql database on a blob field

我的连接模块看起来像这样的Mod_conexionGloval.vb

my conection module looks like this Mod_conexionGloval.vb

'import mysql capabilities
Imports MySql.Data
Imports MySql.Data.Types
Imports MySql.Data.MySqlClient

Module Mod_conexionGlobal
    Public _cadena As String 'almacena los datos de conexin al server
    Public _conexion As New MySqlConnection 'se declara la variable publica para abrir/cerrar la conexion
    Public Function Conexion_Global() As Boolean
        Dim estado As Boolean = True
        Try
            _cadena = ("server=localhost;User Id=jeeves;database=controlrepar;Password=4515")
            _conexion = New MySqlConnection(_cadena)
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
            estado = False
        End Try
        Return estado
    End Function
    Public Sub Cerrar()
        _conexion.Close()
    End Sub
End Module

我的类datos Class_datos.vb,我用来与控件链接以插入编辑或从mysql数据库中删除

my class datos Class_datos.vb wich i use to link with controls insert edit or delete from mysql db

Public Class Class_datos
  Private _resfolio_Rep As Integer
    Private _resid_Cliente As Integer
    Private _resrecep_Date As Date
    Private _resmarca_Equipo As String
    Private _resmodel_Equipo As String
    Private _restipo_Equipo As String
    Private _res_Serie As String
    Private _resdesc_Equipo As String
    Private _foto_Equipo As Byte
    Private _foto_Equipo1 As Byte
    Private _foto_Equipo2 As Byte
    Private _foto_Equipo3 As Byte
    Public Property resfolio_Rep() As Integer
        Get
            Return _resfolio_Rep
        End Get
        Set(ByVal value As Integer)
            _resfolio_Rep = value
        End Set
    End Property
    Public Property resid_Cliente() As Integer
        Get
            Return _resid_Cliente
        End Get
        Set(ByVal value As Integer)
            _resid_Cliente = value
        End Set
    End Property
    Public Property resrecep_Date() As Date
        Get
            Return _resrecep_Date
        End Get
        Set(ByVal value As Date)
            _resrecep_Date = value
        End Set
    End Property
    Public Property resmarca_Equipo() As String
        Get
            Return _resmarca_Equipo
        End Get
        Set(ByVal value As String)
            _resmarca_Equipo = value
        End Set
    End Property
    Public Property resmodel_Equipo() As String
        Get
            Return _resmodel_Equipo
        End Get
        Set(ByVal value As String)
            _resmodel_Equipo = value
        End Set
    End Property
    Public Property restipo_Equipo() As String
        Get
            Return _restipo_Equipo
        End Get
        Set(ByVal value As String)
            _restipo_Equipo = value
        End Set
    End Property
    Public Property res_Serie() As String
        Get
            Return _res_Serie
        End Get
        Set(ByVal value As String)
            _res_Serie = value
        End Set
    End Property
    Public Property resdesc_Equipo() As String
        Get
            Return _resdesc_Equipo
        End Get
        Set(ByVal value As String)
            _resdesc_Equipo = value
        End Set
    End Property
    Public Property foto_Equipo() As Byte
        Get
            Return _foto_Equipo
        End Get
        Set(ByVal value As Byte)
            _foto_Equipo = value
        End Set
    End Property
    Public Property foto_Equipo1() As Byte
        Get
            Return _foto_Equipo1
        End Get
        Set(value As Byte)
            _foto_Equipo1 = value
        End Set
    End Property
    Public Property foto_Equipo2() As Byte
        Get
            Return _foto_Equipo2
        End Get
        Set(value As Byte)
            _foto_Equipo2 = value
        End Set
    End Property
    Public Property foto_Equipo3() As Byte
        Get
            Return _foto_Equipo3
        End Get
        Set(value As Byte)
            _foto_Equipo3 = value
        End Set
    End Property
End Class

Class_ins.vb,我将保存按钮与if一起使用,然后使用functios保存新的或编辑现有的in 保存按钮

Class_ins.vb wich i use the save button with if, and then functios to save new or edit existing in  a save button

'importo mysql drivers
Imports MySql.Data
Imports MySql.Data.Types
Imports MySql.Data.MySqlClient
Public Class Class_ins
    Private _adaptador As New MySqlDataAdapter

'function to insert data on table recepcion res
    Public Function insertarDatos_res(ByVal datos As Class_datos) As Boolean
        Dim estado As Boolean = True
        Try
            Conexion_Global()
            _adaptador.InsertCommand = New MySqlCommand("insert into recepcion(resid_Cliente, resrecep_Date, resmarca_Equipo, resmodel_Equipo, restipo_Equipo, res_Serie, resdesc_Equipo, foto_Equipo, foto_Equipo1, foto_Equipo2, foto_Equipo3) _
values (@resid_Cliente, @resrecep_Date, @resmarca_Equipo, @resmodel_Equipo, @restipo_Equipo, @res_Serie, @resdesc_Equipo, @foto_Equipo, @foto_Equipo1, @foto_Equipo2, @foto_Equipo3", _conexion)
            _adaptador.InsertCommand.Parameters.Add("@resid_Cliente", MySqlDbType.Int32).Value = datos.resid_Cliente
            _adaptador.InsertCommand.Parameters.Add("@resrecep_Date", MySqlDbType.Date).Value = datos.resrecep_Date
            _adaptador.InsertCommand.Parameters.Add("@resmarca_Equipo", MySqlDbType.VarChar, 20).Value = datos.resmarca_Equipo
            _adaptador.InsertCommand.Parameters.Add("@resmodel_Equipo", MySqlDbType.VarChar, 20).Value = datos.resmodel_Equipo
            _adaptador.InsertCommand.Parameters.Add("@restipo_Equipo", MySqlDbType.VarChar, 20).Value = datos.restipo_Equipo
            _adaptador.InsertCommand.Parameters.Add("@res_Serie", MySqlDbType.VarChar, 45).Value = datos.res_Serie
            _adaptador.InsertCommand.Parameters.Add("@resdesc_Equipo", MySqlDbType.VarChar, 500).Value = datos.resdesc_Equipo
            _adaptador.InsertCommand.Parameters.AddWithValue("@foto_Equipo", MySqlDbType.Blob).Value = datos.foto_Equipo
            _adaptador.InsertCommand.Parameters.Add("@foto_Equipo1", MySqlDbType.Blob).Value = datos.foto_Equipo1
            _adaptador.InsertCommand.Parameters.Add("@foto_Equipo2", MySqlDbType.Blob).Value = datos.foto_Equipo2
            _adaptador.InsertCommand.Parameters.Add("@foto_Equipo3", MySqlDbType.Blob).Value = datos.foto_Equipo3
            _conexion.Open()
            _adaptador.InsertCommand.Connection = _conexion
            _adaptador.InsertCommand.ExecuteNonQuery()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
            estado = False
        Finally
            Cerrar()
        End Try
        Return estado
    End Function
    
'function to edit table recepcion'


Public Function actualizarDatosres(ByVal datos As Class_datos) As Boolean
        Dim estado As Boolean = True
        Try
            Conexion_Global()
            _adaptador.UpdateCommand = New MySqlCommand("update recepcion set resid_Cliente=@resid_Cliente, resrecep_date=@resrecep_Date, resmarca_Equipo=@resmarca_Equipo, resmodel_Equipo=@resmodel_Equipo, _
restipo_Equipo=@restipo_Equipo, res_Serie=@res_Serie, resdesc_Equipo=@resdesc_Equipo, foto_Equipo=@foto_Equipo, foto_Equipo1=@foto_Equipo1, foto_Equipo2=@foto_Equipo2, foto_Equipo3=@foto_Equipo3 where resfolio_Rep=@resfolio_Rep", _conexion)
            _adaptador.UpdateCommand.Parameters.Add("@resfolio_Rep", MySqlDbType.Int32).Value = datos.resfolio_Rep
            _adaptador.UpdateCommand.Parameters.Add("@resid_Cliente", MySqlDbType.Int32).Value = datos.resid_Cliente
            _adaptador.UpdateCommand.Parameters.Add("@resrecp_Date", MySqlDbType.Date).Value = datos.resrecep_Date
            _adaptador.UpdateCommand.Parameters.Add("@resmarca_Equipo", MySqlDbType.VarChar, 20).Value = datos.resmarca_Equipo
            _adaptador.UpdateCommand.Parameters.Add("@resmodel_Equipo", MySqlDbType.VarChar, 20).Value = datos.resmodel_Equipo
            _adaptador.UpdateCommand.Parameters.Add("@restipo_Equipo", MySqlDbType.VarChar, 20).Value = datos.restipo_Equipo
            _adaptador.UpdateCommand.Parameters.Add("@res_Serie", MySqlDbType.VarChar, 45).Value = datos.res_Serie
            _adaptador.UpdateCommand.Parameters.Add("@resdesc_Equipo", MySqlDbType.VarChar, 500).Value = datos.resdesc_Equipo
            _adaptador.UpdateCommand.Parameters.Add("@foto_Equipo", MySqlDbType.Blob).Value = datos.foto_Equipo
            _adaptador.UpdateCommand.Parameters.Add("@foto_Equipo1", MySqlDbType.Blob).Value = datos.foto_Equipo1
            _adaptador.UpdateCommand.Parameters.Add("@foto_Equipo2", MySqlDbType.Blob).Value = datos.foto_Equipo2
            _adaptador.UpdateCommand.Parameters.Add("@foto_Equipo3", MySqlDbType.Blob).Value = datos.foto_Equipo3
            _conexion.Open()
            _adaptador.UpdateCommand.Connection = _conexion
            _adaptador.UpdateCommand.ExecuteNonQuery()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
            estado = False
        End Try
        Return estado
    End Function
    'function delete table recepcion res
    Public Function eliminarDatosres(ByVal datos As Class_datos) As Boolean
        Dim estado = True
        Try
            Conexion_Global()
            _adaptador.DeleteCommand = New MySqlCommand("delete from recepcion where resfolio_Rep=@resfolio_Rep", _conexion)
            _adaptador.DeleteCommand.Parameters.Add("@resfolio_Rep", MySqlDbType.Int32).Value = datos.resfolio_Rep
            _conexion.Open()
            _adaptador.DeleteCommand.Connection = _conexion
            _adaptador.DeleteCommand.ExecuteNonQuery()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
            estado = False
        Finally
            Cerrar()
        End Try
        Return estado
    End Function


End Class

现在像往常一样努力将照片保存到表中,但是我陷入了一个难题,从未将图像插入到mysql数据库中

now tring to save photos into the table like i do usually, but i'm gettin into a ptoblem never inserted images into mysql db

所以我在保存"按钮上尝试过

so i tried this on my save button

Dim conexion As New Class_ins
        Dim datos As New Class_datos
        Dim pic1, pic2, pic3, pic4 As New System.IO.MemoryStream()
        PctbImg1.Image.Save(pic1, System.Drawing.Imaging.ImageFormat.Jpeg)
        PctbImg2.Image.Save(pic2, System.Drawing.Imaging.ImageFormat.Jpeg)
        PctbImg3.Image.Save(pic3, System.Drawing.Imaging.ImageFormat.Jpeg)
        PctbImg4.Image.Save(pic4, System.Drawing.Imaging.ImageFormat.Jpeg)

        Dim arrImage() As Byte = pic1.GetBuffer()

        pic1.Close()
        _dtsdatos.Reset()
        datos.resid_Cliente = TxbIdCli.Text
        datos.resrecep_Date = TxbFecha.Text
        datos.resmarca_Equipo = TxbMarca.Text
        datos.resmodel_Equipo = TxbMod.Text
        datos.restipo_Equipo = Txbtipo.Text
        datos.resdesc_Equipo = TxbDesc.Text
        datos.foto_Equipo1 = arrImage <-- message says byte can not be saved as byte


所以我不能称其为保存例行程序,甜菜为我们提供一些解决方法,但是我一个人看不见遮篷,请帮忙


so i cant call my save routine wich, beet tinking for a few ours how to correct it, but i alone am not able to see the awnser, please help

推荐答案

尝试将 _foto_Equipo1 的定义从单字节更改为数组,即  将_foto_Equipo1作为Byte()专用" ,然后对 foto_Equipo1 属性,参数等.也请使用'pic1.ToArray()'代替 "pic1.GetBuffer()" .

Try changing the definition of _foto_Equipo1 from single byte to array, i.e. ‘Private _foto_Equipo1 As Byte( )’, and make other corresponding adjustments to foto_Equipo1 property, parameters, etc. Also use ‘pic1.ToArray()’ instead of ‘pic1.GetBuffer()’.


这篇关于将图像保存到mysql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆