Dopo varie peripezie e aver consultato un bel po di forum sul .NET framework sono riuscito a tirare fuori la procedura per inserire queste benedette immagini nel database MySQL.
condivido qst mia conoscenza con tutti gli utenti del forum.
...... e non mi dite che c'era gia qlc in giro, xke NON HO TROVATO NIENTE!!!!
tendo a precisare che il codice è scritto per VB.NET !!!! non so se è possibile convertire tutto in vb6 ?!?
allora.... comincio col dirvi che è stato necessario scaricare un driver di terzi (gratuito)
questo componente è praticamente uguale al componente OLEDB !!! solo per MySQL.
fatto questo aprite il progetto
inserite il riferimento a: "CoreLab.MySql" che dopo l'installazione si trova in "C:\Programmi\CoreLab\MySQLDirect.NET\CoreLab.MySq l.dll"
poi nel codice potete inserire all'inizio un bel
codice:
Imports CoreLab.MySql
mentre questo è il codice per uplodare l'immagine nel database
codice:
Public Function InsertImage(ByVal ImgPath As String, ByVal FieldName As String, ByVal Sql As String) As Boolean
Try
Dim objconn As New Odbc.OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DB_SERVER & ";DATABASE=" & DB_NAME & ";USER=" & DB_USER & ";PASSWORD=" & DB_PWD & ";OPTION=3;")
objconn.Open()
Dim stream As System.IO.FileStream
Dim flength As System.Int32 = 0
stream = New System.IO.FileStream(ImgPath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
flength = CInt(stream.Length)
Dim buffer(flength) As Byte
stream.Read(buffer, 0, flength)
Dim cmd As New Odbc.OdbcCommand(Sql, objconn)
Dim param As Odbc.OdbcParameter = New Odbc.OdbcParameter(FieldName, Odbc.OdbcType.Binary)
param.Value = buffer
cmd.Parameters.Add(param)
cmd.ExecuteNonQuery()
cmd = Nothing
stream.Close()
objconn.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function LoadImage(ByVal FieldName As String, ByVal Sql As String) As System.Drawing.Image
Dim img As System.Drawing.Image
Try
Dim objconn As New Odbc.OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DB_SERVER & ";DATABASE=" & DB_NAME & ";USER=" & DB_USER & ";PASSWORD=" & DB_PWD & ";OPTION=3;")
objconn.Open()
Dim cmd As New Odbc.OdbcCommand(Sql, objconn)
Dim myReader As Odbc.OdbcDataReader = cmd.ExecuteReader()
While myReader.Read()
Dim buffer() As Byte
buffer = myReader(FieldName)
Dim stream As New System.IO.FileStream(Path.GetTempPath & "img.tmp", System.IO.FileMode.Create)
stream.Write(buffer, 0, buffer.Length)
img = img.FromStream(stream)
stream.Close()
Dim f As File
If f.Exists(Path.GetTempPath & "img.tmp") = True Then f.Delete(Path.GetTempPath & "img.tmp")
End While
myReader.Close()
cmd = Nothing
objconn.Close()
Catch ex As Exception
Return Nothing
End Try
Return img
End Function
N.B. :
database: test
tabella: myimages
campi: ID (int)
ImgField (blob)
mi manca la procedura per riprendere l'immagine.... ma qnd la completo la posto subito.....
SE AVETE MIGLIORIE scrivetele subito perchè ho intenzione di creare una bella guida per i prossimi utenti
CIAO