Hilfe bei Code Binärumwandlung

Luda

Erfahrenes Mitglied
Hallo ich habe Folgenden Code aus dem Netz. Nur wie schick ich das ganze in meine DB?

Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (lpTo As Any, lpFrom As Any, _
ByVal lLen As Long)

Dim B() As Byte, bildPfad As String

Private Sub cmd_ok_Click()
Dim rs As ADODB.Recordset, sql As String
Dim aa$, l&
sql = "select * from Bilder where fahr_nr = 1"
Set rs = SQL_recordset(cn, sql)

'Einlesen als String
Open bildPfad For Binary As #1
aa = Input(LOF(1), #1)
Close 1

'Umwandeln des Strings in ein ByteArray
l = Len(aa) - 1
ReDim B(0 To l)
Call CopyMemory(B(0), ByVal aa, l + 1)

' hier müsste jetzt der code für in die db zu schreiben kommen.
'z.B.
' rs.fields("bild").value = ...................

rs.Update
rs.Close
Set rs = Nothing
Unload Me
End Sub
 
Das hat mir etwas weitergeholfen. Musste das ganze etwas umstricken.
Das speichern geht ohne Fehler.
Wenn ich jetzt die datei Laden möchte ist nichts in der datei temp.bmp.
Bekomme an der stelle:

Image1.Picture = LoadPicture("C:\Temp.bmp")
in LoadPictureFromDB
die Meldung unerwarteter Fehler.

Option Explicit

Dim rstRecordset As ADODB.Recordset
Dim cnnConnection As ADODB.Connection
Dim strStream As ADODB.Stream
Dim BildPfad As String


Public Function LoadPictureFromDB(rs As ADODB.Recordset)

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open

strStream.Write rs.Fields("Bild").Value

strStream.SaveToFile "C:\Temp.bmp", adSaveCreateOverWrite
Image1.Picture = LoadPicture("C:\Temp.bmp")
Kill ("C:\Temp.bmp")
LoadPictureFromDB = True

End Function

Public Function SavePictureToDB(rs As ADODB.Recordset, _
sFileName As String)

Dim oPict As StdPicture
Set oPict = LoadPicture(sFileName)

rs.AddNew

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile sFileName
rs.Fields("Bild").Value = strStream.Read

Image1.Picture = LoadPicture(sFileName)
SavePictureToDB = True

rs.Update
End Function

Private Sub cmd_Abruf_Click()
LoadPictureFromDB rstRecordset
End Sub

Private Sub cmd_ok_Click()
SavePictureToDB rstRecordset, BildPfad
End Sub

Private Sub cmdEinfügen_Click() ' Hier wird cmDialog geöffnet und pfad ausgewählt
On Error Resume Next
With cmDialog
.CancelError = True
.DialogTitle = "Datei Öffnen"
.Filter = _
"Jpegs(*.jpg)|*.jpg|Bitmaps(*.bmp)|*.bmp"
.ShowOpen

End With
BildPfad = cmDialog.FileName
On Error GoTo 0
Err.Number = 0
End Sub


Private Sub Form_Load()
Set cnnConnection = New ADODB.Connection
Set rstRecordset = New ADODB.Recordset

cnnConnection.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=213.77.76.92;" & _
"DATABASE=base; " & _
"User Id=db1;Password=mypass;" & _
"OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384

rstRecordset.Open "Select * from Bilder", cnnConnection, adOpenKeyset, adLockOptimistic


End Sub
 
Ich habe jetzt einen Code,
der bei access ohne Probleme funktioniert.
Bei mySql bekomme ich nichts ind die db geschrieben.

Option Explicit
Dim cn As ADODB.Connection, rs As ADODB.Recordset, bildPfad As String
' Bild in Variable (String) lesen
Public Function ReadPicture(sFilename As String) _
As String

Dim F As Integer
Dim sInhalt As String

F = FreeFile
Open sFilename For Binary As #F
sInhalt = Space$(LOF(F))
Get #F, , sInhalt
Close #F

ReadPicture = sInhalt
End Function

Private Sub cmdAnzeigen_Click()
ShowPicture (rs!bild)
End Sub

Private Sub Command1_Click()
On Error Resume Next
With cmDialog
.CancelError = True
.DialogTitle = "Datei Öffnen"
.Filter = _
"Jpegs(*.jpg)|*.jpg|Bitmaps(*.bmp)|*.bmp"
.ShowOpen

End With
bildPfad = cmDialog.FileName
rs.AddNew
rs.Fields("bild").Value = ReadPicture(bildPfad)
rs.Update
On Error GoTo 0
Err.Number = 0
End Sub

' Bild aus Datenbank laden und anzeigen
Public Sub ShowPicture(ByVal sInhalt As String)

Dim F As Integer

' Bilddatei temporär erstellen
F = FreeFile
Open App.Path & "\Bild.tmp" For Output As #F
Print #F, sInhalt;
Close #F

' Bild anzeigen
Image1.Picture = LoadPicture(App.Path & "\Bild.tmp")

' temporäre Datei löschen
Kill App.Path & "\Bild.tmp"
End Sub


Private Sub Form_Load()
Set cn = SQL_connect 'ODBC
Set rs = SQL_recordset(cn, "select * from Bilder") 'ODBC

End Sub



'-----verbindung zu DB ------------

Public Function SQL_connect() As ADODB.Connection
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
& "SERVER=233.33.11.111;" _
& "DATABASE=xxx;" _
& "UID=xxx;" _
& "PWD=xxxx;" _
& "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384

conn.CursorLocation = adUseClient
conn.Open
Set SQL_connect = conn

End Function

Public Function SQL_recordset(conn As ADODB.Connection, sql As String) As ADODB.Recordset

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim mystream As ADODB.Stream

Set mystream = New ADODB.Stream
mystream.Type = adTypeBinary
rs.Open sql, conn, adOpenStatic, adLockOptimistic
mystream.Open
Set SQL_recordset = rs
End Function
 

Neue Beiträge

Zurück