简体   繁体   中英

updating a record using sql in vb.net

I've included the code below. I have a record in sql that I want to update. I keep getting the message

incorrect syntax near ")"

I have tried just about everything I can think of, and I don't see anything wrong, can someone please tell me what it is?

    Sub UpdateData(ByVal PHASE1_COUNT_VAR As String, ByVal PHASE1_GOAL_VAR As String, ByVal PHASE1_PERCENTAGE_VAR As String, ByVal PHASE2_COUNT_VAR As String, ByVal PHASE2_GOAL_VAR As String, ByVal PHASE2_PERCENTAGE_VAR As String, ByVal PLANT_COUNT_VAR As String, ByVal PLANT_GOAL_VAR As String, ByVal PLANT_PERCENTAGE_VAR As String, ByVal SHIFT_VAR As String, ByVal CURRENT_DATETIME_VAR As String)
    Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "Data Source=MESTEST\ONLINE;Integrated Security=False;User ID=SPIRE_SUN_SIMULATOR;Password=SPIRE_SUN_SIMULATOR;Connect TimeOut=15;Encrypt=False;TrustServerCertificate=False"
    Dim INDEX_NUM_VAR As Integer = 1
    Dim myCommand As SqlCommand
    'parametrized update sql command
    Try
        sqlconn.Open()
        myCommand = New SqlCommand("UPDATE dbo.PRODUCTION_DATA SET PHASE1_COUNT = @PHASE1_COUNT, PHASE1_GOAL = @PHASE1_GOAL, PHASE1_PERCENTAGE = @PHASE1_PERCENTAGE, PHASE2_COUNT = @PHASE2_COUNT, PHASE2_GOAL = @PHASE2_GOAL, PHASE2_PERCENTAGE = @PHASE2_PERCENTAGE, PLANT_COUNT = @PLANT_COUNT, PLANT_GOAL = @PLANT_GOAL, PLANT_PERCENTAGE = @PLANT_PERCENTAGE, SHIFT = @SHIFT, CURRENT_DATETIME = @CURRENT_DATETIME where INDEX_NUM = @INDEX_NUM)", sqlconn)
        myCommand.Parameters.AddWithValue("@PHASE1_COUNT", PHASE1_COUNT_VAR)
        myCommand.Parameters.AddWithValue("@PHASE1_GOAL", PHASE1_GOAL_VAR)
        myCommand.Parameters.AddWithValue("@PHASE1_PERCENTAGE", PHASE1_PERCENTAGE_VAR)
        myCommand.Parameters.AddWithValue("@PHASE2_COUNT", PHASE2_COUNT_VAR)
        myCommand.Parameters.AddWithValue("@PHASE2_GOAL", PHASE2_GOAL_VAR)
        myCommand.Parameters.AddWithValue("@PHASE2_PERCENTAGE", PHASE2_PERCENTAGE_VAR)
        myCommand.Parameters.AddWithValue("@PLANT_COUNT", PLANT_COUNT_VAR)
        myCommand.Parameters.AddWithValue("@PLANT_GOAL", PLANT_GOAL_VAR)
        myCommand.Parameters.AddWithValue("@PLANT_PERCENTAGE", PLANT_PERCENTAGE_VAR)
        myCommand.Parameters.AddWithValue("@SHIFT", SHIFT_VAR)
        myCommand.Parameters.AddWithValue("@CURRENT_DATETIME", CURRENT_DATETIME_VAR)
        myCommand.Parameters.AddWithValue("@INDEX_NUM", INDEX_NUM_VAR)
        myCommand.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

There's a redundant closing parenthesis at the end of the update SQL, remove it:

Dim cmdText As String = <![CDATA[
UPDATE dbo.production_data 
SET    phase1_count = @PHASE1_COUNT, 
       phase1_goal = @PHASE1_GOAL, 
       phase1_percentage = @PHASE1_PERCENTAGE, 
       phase2_count = @PHASE2_COUNT, 
       phase2_goal = @PHASE2_GOAL, 
       phase2_percentage = @PHASE2_PERCENTAGE, 
       plant_count = @PLANT_COUNT, 
       plant_goal = @PLANT_GOAL, 
       plant_percentage = @PLANT_PERCENTAGE, 
       shift = @SHIFT, 
       current_datetime = @CURRENT_DATETIME 
WHERE  index_num = @INDEX_NUM 
]]>.Value
myCommand = New SqlCommand(cmdText, sqlconn)

(I've used the VB.NET fake of a verbatim string literal for better readability. You need to add Imports System.Xml.Linq )

As the error says:

INDEX_NUM = @INDEX_NUM)

There's a trailing )

Also, for convention you shouldn't use UPPERCASE for variables, but for constants. And there's no need to postfix them with _VAR. And the connection should be closed. And the exception is too general. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM