locked
vb.net - how to update an existing database RRS feed

  • Question

  • Hi,

    I'm supposed to do an application which updates an existing database in its same directory.

    Here is my code that seems not working:

    Public Class Form1
        Dim res As String = DateTime.Now.ToString("dd/mm/yyyy")
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= " & CurDir() & "\" & "19QM002.mdf"
        Dim conn As SqlConnection
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Close()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Using conn = New SqlConnection(constr)
                    Dim command As New SqlCommand("UPDATE       [Linea Montaggio GDI 1]
    SET                [Data Ingresso In Linea] = @Data_Ingresso_In_Linea, [Ora Ingresso In Linea] = @Ora_Ingresso_In_Linea, [Numero Pallet] = @Numero_Pallet, [Numero Nido] = @Numero_Nido, [Stazione Scarto] = @Stazione_Scarto, 
                             Esito = @Esito, [Codice Scarto] = @Codice_Scarto, Tipo = @Tipo, [Valore Accostamento ST3] = @Valore_Accostamento_ST3, 
                             [Valore Accostamento ST3 Con Correzione Corsa] = @Valore_Accostamento_ST3_Con_Correzione_Corsa, [Valore Accostamento ST4] = @Valore_Accostamento_ST4, 
                             [Valore Accostamento ST4 Con Correzione Corsa] = @Valore_Accostamento_ST4_Con_Correzione_Corsa, [Esito Tenuta] = @Esito_Tenuta, [Testa di Tenuta] = @Testa_di_Tenuta, [Carico Piantaggio] = @Carico_Piantaggio, 
                             [Quota Piantaggio 1] = @Quota_Piantaggio_1, [Quota Piantaggio 2] = @Quota_Piantaggio_2, Corsa = @Corsa, Datamatrix = @Datamatrix, [Linea Spillo] = @Linea_Spillo, [Linea Fondello] = @Linea_Fondello, 
                             [Orario Di Uscita] = @Orario_Di_Uscita", conn)
                    command.Parameters.Add("@Corsa", SqlDbType.Real).Value = ("@corsa" * (1000))
                    conn.Open()
                    conn.Close()
                End Using
            Catch ex As System.Exception
            End Try
        End Sub
    End Class

    What's wrong with it?

    Thank you for support.

    Thursday, June 27, 2019 2:08 PM

Answers

  • Hi,

    missing execution code,add code:

    command.ExecuteNonQuery()

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by pb1990 Friday, June 28, 2019 8:30 AM
    Friday, June 28, 2019 6:40 AM

All replies

  • Hello,

    For each parameter in the UPDATE such as Tipo = @Tipo you need to do a command.Parameter.Add, currently you have but one parameter and need one for each parameter in the UPDATE statement.

    Example using a class

    Example using a DataRow


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    • Edited by KareninstructorMVP Thursday, June 27, 2019 3:24 PM Added links to code samples
    Thursday, June 27, 2019 3:10 PM
  • Hi,

    missing execution code,add code:

    command.ExecuteNonQuery()

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by pb1990 Friday, June 28, 2019 8:30 AM
    Friday, June 28, 2019 6:40 AM