Daten in SQL-DB schreiben?

HM644

Grünschnabel
Hallo Community,
nachdem ich schon seit Tagen nicht weiter komme, habe ich mich hier mal angemeldet.
Ich hoffe Ihr könnt mir auf die Sprünge helfen.
Ich versuche mich in VB2005Express und SQL-Server2005Express einzuarbeiten. Habe früher schon ein paar Sachen mit VB6 gemacht.
Folgendes Problem:
Ich will in einem Formular ein paar Labelfelder in eine SQL-DB eintragen. Aber das will einfach nicht funktionieren.
Bin beim stöbern hier im Forum auch auf folgenden Link gestossen:
http://www.galileocomputing.de/open...l_26-003.htm#9c24335046a9761bef6eb6d1470daf93

Habe das Beispiel angepasst, aber irgendwie erfolgt keine Reaktion. Wenn ich anschliessend in die Datenbank schaue steht nichts drin.
Hier mein Code: (Die Variable B_ID wird vorher Systemweit deklariert)

Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim strDateiName As String
        Dim intBildAnzahl, intBildStelle, intB_ID As Integer
        strDateiName = CStr(Label9.Text)
        intBildAnzahl = CInt(Label10.Text)
        intBildStelle = CInt(Label6.Text)
        intB_ID = B_ID


        Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\HM\Documents\Visual Studio 2005\Projects\FarbWechsler\FarbWechsler\Bilder.mdf"";Integrated Security=True;database=Bilder;Connect Timeout=30;User Instance=True")
        Dim strSQL As String = "INSERT INTO Name(B_ID, DateiName, BildAnzahl, BildStelle) VALUES(intB_ID, strDateiName, intBildAnzahl, intBildStelle)"
        Try
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            Console.WriteLine("Fehlermeldung: {0}", ex.Message)
        End Try
        con.Close()
        B_ID += 1
    End Sub
 
Zuletzt bearbeitet:
OK, dieser Problem habe ich gelöst...:rolleyes:
Es lag mal wieder an den Variablendefinitionen(im weitesten Sinne), hier der neue Code:
Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim strDateiName As String
        Dim intBildAnzahl, intBildStelle, intB_ID As Integer
        strDateiName = CStr(Label9.Text)
        intBildAnzahl = CInt(Label10.Text)
        intBildStelle = CInt(Label6.Text)
        intB_ID = bID

        Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\HM\Documents\Visual Studio 2005\Projects\FarbWechsler\FarbWechsler\Bilder.mdf"";Integrated Security=True;database=Bilder;Connect Timeout=30;User Instance=True")
        Dim strSQL As String = "INSERT INTO Name(B_ID, DateiName, BildAnzahl, BildStelle) VALUES('" + CStr(intB_ID) + "', '" + strDateiName + "', '" + CStr(intBildAnzahl) + "', '" + CStr(intBildStelle) + "')"
        Try
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        con.Close()
        bID += 1
    End Sub
Aber nachdem das t, bin ich gleich auf ein neues Problem gestossen:
Ich muss in meiner DB auch Bilder (Images) speichern. Dazu nehme ich diesen Code:
Code:
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim imgBild As Image
        Dim intB_ID As Integer
        imgBild = PictureBox1.Image
        intB_ID = bID

        Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\HM\Documents\Visual Studio 2005\Projects\FarbWechsler\FarbWechsler\Bilder.mdf"";Integrated Security=True;database=Position;Connect Timeout=30;User Instance=True")
        Dim strSQL As String = "INSERT INTO Position(B_ID, Bild1) VALUES('" + CStr(intB_ID) + "', 'imgBild')"
        Try
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        con.Close()
        bID += 1
    End Sub
Das einfügen klappt auch ohne Fehlermeldung, aber wenn ich dann das Bild aus der DB abrufen will, bleibt die verbundene 'PictureBox' leer.
In der Beispieldatenbank "Northwind" sind auch Bilder gespeichert.
Wie geht das...?
 
Zurück