Announcement

Collapse
No announcement yet.

Bilder speichern in mysql über VB6

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bilder speichern in mysql über VB6

    Hallo,

    Ich möchte gerne Bilder in MySQL speichern.

    Mein Quellcode sieht folgendermaßen aus:

    <pre>
    Private Sub cmd_Bild_speichern_Click()
    Dim Bild_Pfad As String 'Variable zum Speichern des Pfades
    Dim SQL_Befehl As String 'Variable zum Speichern des SQL_Befehles
    Dim RS_Bild_S As ADODB.Recordset 'Recordset zum Absetzen des SQL_Befehls
    Dim Doc_Bild_S As ADODB.Stream 'Stream zum Speichern des Bildes
    Dim conn_Bild As ADODB.Connection 'Spezielle Verbindung zum Bild-Speichern

    'Falls Pfad falsch
    On Error GoTo Fehler_Pfad
    Bild_Pfad = Right$(Label10.Caption, Len(Label10.Caption) - 6)
    On Error GoTo 0
    'Falls Pfad = ""
    If Bild_Pfad = "" Then
    MsgBox Fehler_Bild_Mld, vbCritical, Fehler_Bild_Titel
    Exit Sub
    End If

    'spezeille Verbindung öffnen
    Set conn_Bild = New ADODB.Connection
    With conn_Bild
    .ConnectionString = "DRIVER={" & sSQLDB_DRV & "};" _
    & "SERVER=" & sSQLDB_SRV & ";" _
    & "DATABASE=" & sSQLDB_DAT & ";" _
    & "UID=" & sSQLDB_UID & ";" _
    & "PWD=" & sSQLDB_PWD & ";" _
    & "OPTION=" & 2 + 8 + 16384
    .CursorLocation = adUseServer
    .Open
    End With

    'Stream öffnen
    Set Doc_Bild_S = New ADODB.Stream
    With Doc_Bild_S
    .Type = adTypeBinary
    .Open
    .LoadFromFile Bild_Pfad
    End With

    'SQL_Befehl zusammensetzen
    SQL_Befehl = "Select * from werkzeug where kat01 = " & _
    Left$(Combo_Kat01.Text, 1) & " and kat02 = " & _
    Left$(Combo_Kat02.Text, 2) & " and kat03 = " & _
    Left$(Combo_Kat03.Text, 2) & " and w_nr = " & _
    txt_W_Nr.Text & ";"
    'RS öffnen
    Set RS_Bild_S = New ADODB.Recordset
    With RS_Bild_S
    .Open SQL_Befehl, conn_Bild, adOpenDynamic, adLockOptimistic

    'Update durchführen
    .Fields!w_Bild_url = Doc_Bild_S.Read
    .Update
    .Close
    End With

    'steam Schließen
    Doc_Bild_S.Close
    'Pfad aus Label10 löschen
    Label10.Caption = "Pfad: "
    'Verbindung schließen
    conn_Bild.Close
    Exit Sub

    'Fehlerbehandlung
    Fehler_Pfad:
    MsgBox Fehler_Bild_Mld, vbCritical, Fehler_Bild_Titel
    </pre>

    In den Zeilen <b>.Fields!w_Bild_url = doc_Bild_S.Read</B> bzw. <B>.Update</B> treten Fehler auf, je nachdem wie ich die Verbindung, bzw. das Recordset öffne.

    Ich hoffe, jemand kann mir helfen.
    Danke im Voraus.

    Gruß
    tobias

  • #2
    Hallo nochmal,

    ich habe jetzt meinen Code umgestellt. Jetzt kann ich Bilder speichern, die kleiner bzw. gleich groß wie das bereits gespeicherte sind.

    <B>Hierfür muß ich das erste Bild pro Datensatz im MySQL-Control-Center abspeichern.</B> Das ist eigendlich nicht, das was ich damit erreichen wollte. Vielleicht kann mir jemand sagen, wie ich auch <i>größere Bilder und das 1. Bild pro Datensatz</I> mit meinem Programm speichern kann.

    Hier ist mein momentaner Quellcode:
    <Pre>Private Sub cmd_Bild_speichern_Click()
    Dim Bild_Pfad As String 'Variable zum Speichern des Pfades
    Dim SQL_Befehl As String 'Variable zum Speichern des SQL_Befehles
    Dim RS_Bild_S As ADODB.Recordset 'Recordset zum Absetzen des SQL_Befehls
    Dim Doc_Bild_S As ADODB.Stream 'Stream zum Speichern des Bildes
    Dim conn_Bild As ADODB.Connection 'Spezielle Verbindung zum Bild-Speichern

    'Falls Pfad falsch
    On Error GoTo Fehler_Pfad
    Bild_Pfad = Right$(Label10.Caption, Len(Label10.Caption) - 8)
    On Error GoTo 0
    'Falls Pfad = ""
    If Bild_Pfad = "" Then
    MsgBox Fehler_Bild_Mld, vbCritical, Fehler_Bild_Titel
    Exit Sub
    End If

    'spezeille Verbindung öffnen
    Set conn_Bild = New ADODB.Connection
    With conn_Bild
    .ConnectionString = "DRIVER={" & sSQLDB_DRV & "};" _
    & "SERVER=" & sSQLDB_SRV & ";" _
    & "DATABASE=" & sSQLDB_DAT & ";" _
    & "UID=" & sSQLDB_UID & ";" _
    & "PWD=" & sSQLDB_PWD & ";" _
    & "OPTION=" & 2 + 8 + 32 + 2048 + 16384
    .CursorLocation = adUseClient
    .Open
    End With

    'Stream öffnen
    Set Doc_Bild_S = New ADODB.Stream
    With Doc_Bild_S
    .Type = adTypeBinary
    .Open
    .LoadFromFile Bild_Pfad
    End With

    'SQL_Befehl zusammensetzen
    SQL_Befehl = "Select kat01,kat02,kat03,w_nr,w_bild_url from werkzeug where kat01 = " & _
    Left$(Combo_Kat01.Text, 1) & " and kat02 = " & _
    Left$(Combo_Kat02.Text, 2) & " and kat03 = " & _
    Left$(Combo_Kat03.Text, 2) & " and w_nr = " & _
    txt_W_Nr.Text & ";"
    'RS öffnen
    Set RS_Bild_S = New ADODB.Recordset
    With RS_Bild_S
    .ActiveConnection = conn_Bild
    .CursorLocation = adUseServer
    .LockType = adLockOptimistic
    .CursorType = adOpenDynamic
    .Source = SQL_Befehl
    .Open
    'Update durchführen
    .Fields("w_Bild_url").Value = Doc_Bild_S.Read
    .Update
    .Close
    End With

    'steam Schließen
    Doc_Bild_S.Close
    'Pfad aus Label10 löschen
    Label10.Caption = "Pfad: "
    'Verbindung schließen
    conn_Bild.Close
    Exit Sub

    'Fehlerbehandlung
    Fehler_Pfad:
    MsgBox Fehler_Bild_Mld, vbCritical, Fehler_Bild_Titel
    End Sub</Pre>

    Gruß
    tobia

    Comment

    Working...
    X