[GELÖST] Mysql über IF Befehl

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Kevin.

    [GELÖST] Mysql über IF Befehl

    Hallo ich habe ein Problem ich möchte wenn in Textbox2 kein Text ist das er ein Text mit :

    VB.NET-Quellcode

    1. myInsertQuery = "INSERT INTO einsatz (einsatz) Values('" & TextBox1.Text & "')"
    sendet ist aber in Textbox2 ein Text dann soll er den Befehl machen

    VB.NET-Quellcode

    1. myInsertQuery = "UPDATE einsatz SET einsatz = REPLACE('*','*', '" & TextBox1.Text & "')"


    Info :

    VB.NET-Quellcode

    1. If TextBox2.Text = Text Then
    2. Dim myconnectionstring As String = ""
    3. If myconnectionstring = "" Then
    4. myconnectionstring = "server=ip;" & "user id=un;" & "password=pw;" & "database=db"
    5. End If
    6. Dim myConnection As New MySqlConnection(myconnectionstring)
    7. Dim myInsertQuery As String
    8. myInsertQuery = "UPDATE einsatz SET einsatz = REPLACE('*','*', '" & TextBox1.Text & "')"
    9. Dim myCommand As New MySqlCommand(myInsertQuery)
    10. myCommand.Connection = myConnection
    11. myConnection.Open()
    12. myCommand.ExecuteNonQuery()
    13. myConnection.Close()
    14. ElseIf Not TextBox2.SelectedText Then
    15. Dim myconnectionstring As String = ""
    16. If myconnectionstring = "" Then
    17. myconnectionstring = "server=ip;" & "user id=un;" & "password=pw;" & "database=db"
    18. End If
    19. Dim myConnection As New MySqlConnection(myconnectionstring)
    20. Dim myInsertQuery As String
    21. myInsertQuery = "INSERT INTO einsatz (einsatz) Values('" & TextBox1.Text & "')"
    22. Dim myCommand As New MySqlCommand(myInsertQuery)
    23. myCommand.Connection = myConnection
    24. myConnection.Open()
    25. myCommand.ExecuteNonQuery()
    26. End If

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Kevin“ () aus folgendem Grund: Thema Gelöst

    also wenn in

    VB.NET-Quellcode

    1. Textbox2
    ein Text also wenn entwas dirn steht dann soll er

    VB.NET-Quellcode

    1. myInsertQuery = "UPDATE einsatz SET einsatz = REPLACE('*','*', '" & TextBox1.Text & "')"


    diesen BEFHEL machen aber wenn in Textbox2 kein Text ist also in dem TextFeld dann soll er diesen BEFHEL machen :

    VB.NET-Quellcode

    1. myInsertQuery = "INSERT INTO einsatz (einsatz) Values('" & TextBox1.Text & "')"



    mein Code geh aber net ;

    VB.NET-Quellcode

    1. If TextBox2.Text = Text Then
    2. Dim myconnectionstring As String = ""
    3. If myconnectionstring = "" Then
    4. myconnectionstring = "server=ip;" & "user id=ui;" & "password=pw;" & "database=db"
    5. End If
    6. Dim myConnection As New MySqlConnection(myconnectionstring)
    7. Dim myInsertQuery As String
    8. myInsertQuery = "UPDATE einsatz SET einsatz = REPLACE('*','*', '" & TextBox1.Text & "')"
    9. Dim myCommand As New MySqlCommand(myInsertQuery)
    10. myCommand.Connection = myConnection
    11. myConnection.Open()
    12. myCommand.ExecuteNonQuery()
    13. myConnection.Close()
    14. ElseIf Not TextBox2.SelectedText Then
    15. Dim myconnectionstring As String = ""
    16. If myconnectionstring = "" Then
    17. myconnectionstring = "server=ip;" & "user id=ui;" & "password=pw;" & "database=db"
    18. End If
    19. Dim myConnection As New MySqlConnection(myconnectionstring)
    20. Dim myInsertQuery As String
    21. myInsertQuery = "INSERT INTO einsatz (einsatz) Values('" & TextBox1.Text & "')"
    22. Dim myCommand As New MySqlCommand(myInsertQuery)
    23. myCommand.Connection = myConnection
    24. myConnection.Open()
    25. myCommand.ExecuteNonQuery()
    26. End If
    Wieso packst du die komplette Verbindung mit in die IF Bedingung wenn doch dort alles gleich bleibt? Ist doch logisch dass du in die IF nur die Änderungen reinpacken musst. Hier mal der PseudoCode:

    Datenbankverindung aufbauen

    Überprüfe ob Textbox2 = Text ist
    Wenn ja Befülle den SQL String mit diesem Query
    Wenn nein Befülle den SQL String mit einem anderen Query

    Ausführen des SQL Strings