在vb.net中使用sql更新记录

在vb.net中使用sql更新记录,sql,sql-server,vb.net,Sql,Sql Server,Vb.net,我已经包括了下面的代码。我有一个sql记录要更新。我一直在得到消息 “附近的语法不正确” 我已经尝试了我能想到的一切,我没有发现任何错误,有人能告诉我是什么吗 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 PHASE

我已经包括了下面的代码。我有一个sql记录要更新。我一直在得到消息

“附近的语法不正确”

我已经尝试了我能想到的一切,我没有发现任何错误,有人能告诉我是什么吗

    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

更新SQL末尾有一个多余的右括号,请将其删除:

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)
Dim cmdText作为字符串=.Value
myCommand=New-SqlCommand(cmdText,sqlconn)
(为了更好的可读性,我使用了一个逐字字符串文字的VB.NET伪文本。您需要添加
Imports System.Xml.Linq

,因为错误显示:

INDEX_NUM = @INDEX_NUM)
有一个尾随的


此外,按照惯例,变量不应使用大写字母,而应使用常量。不需要用_VAR对它们进行后缀,连接应该关闭。例外情况太普遍了。:)

完全正确-从INDEX_NUM=@INDEX_NUM)@n8wrl中去掉结束括号@n8wrl我没有跟着你,那么结束括号会放在哪里呢?还是我错过了什么?我现在看到了,萝莉失明了,现在我看到了。