尝试更新VB.net中的列值时出错

时间:2014-08-08 01:12:24

标签: asp.net sql-server vb.net sql-update

美好的一天!

我只是想向你们寻求帮助,我再次遇到UPDATE功能问题,这是我的代码......

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    con = New SqlConnection("Server=localhost\SQLEXPRESS;Database=Vehicle;Trusted_Connection=True;")
    con.Open()

    Dim cmd As SqlCommand = con.CreateCommand

    cmd.CommandText = String.Format("UPDATE trip SET ticketno, charge, driver, destination, passenger, purpose, tripdate", txtticket.Text, txtcharge.Text, txtdriver.Text, txtdestination.Text, txtpassenger.Text, txtpurpose.Text, dtptripdate1.Value)

    Dim affectedRows As Integer = cmd.ExecuteNonQuery
    If affectedRows > 0 Then
        MsgBox("Succesfully Updated")
    Else
        MsgBox("Failed to update")
    End If

    con.Close()
End Sub

当我尝试单击按钮时,会显示错误:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

其他信息:','附近的语法不正确。

谢谢你帮助我。 我真的很接近为我的办公室完成这个小规模的项目,但我遇到了这些问题。

2 个答案:

答案 0 :(得分:0)

您的UPDATE声明有误,所以您的String.Format。不要错过WHERE条件吗?

cmd.CommandText = String.Format("UPDATE trip SET ticketno='{0}', charge='{1}', driver='{2}', destination='{3}', passenger='{4}', purpose='{5}', tripdate='{6}'", txtticket.Text, txtcharge.Text, txtdriver.Text, txtdestination.Text, txtpassenger.Text, txtpurpose.Text, dtptripdate1.Value)

或者尝试这样做以避免SQL注入。 用Using块包裹你的conn以确保处理你的SqlConnection

Using conn As New SqlConnection("Server=localhost\SQLEXPRESS;Database=Vehicle;Trusted_Connection=True;")
    conn.Open()
    Dim cmd As New SqlCommand
    cmd.Connection = conn
    cmd.CommandText = "UPDATE trip SET ticketno=@ticketno, charge=@charge, driver=@driver, destination=@destination, " & _
                      "passenger=@passenger, purpose=@purpose, tripdate=@tripdate " & _
                      "WHERE SomeCondition"
    cmd.Parameters.AddWithValue("@ticketno", txtticket.Text)
    cmd.Parameters.AddWithValue("@charge", txtcharge.Text)
    cmd.Parameters.AddWithValue("@driver", txtdriver.Text)
    cmd.Parameters.AddWithValue("@destination", txtdestination.Text)
    cmd.Parameters.AddWithValue("@passenger", txtpassenger.Text)
    cmd.Parameters.AddWithValue("@purpose", txtpurpose.Text)
    cmd.Parameters.AddWithValue("@tripdate", dtptripdate1.Value)

    Dim affectedRow As Integer = cmd.ExecuteNonQuery
    IIf(affectedRow > 0, MsgBox("Succesfully Updated"), MsgBox("Failed to update"))
End Using

答案 1 :(得分:0)

    Try this,   

   Dim myCommand = New SqlCommand("UPDATE trip SET ticketno=@ticketno, charge=@charge, driver=@driver, destination=@destination, passenger=@passenger, purpose=@purpose, tripdate=@tripdate",con)

        myCommand.Parameters.Add("@ticketno", txtticket.Text)
        myCommand.Parameters.Add("@charge", txtcharge.Text)
        myCommand.Parameters.Add("@driver", txtdriver.Text)
        myCommand.Parameters.Add("@destination", txtdestination.Text)
        myCommand.Parameters.Add("@passenger", txtpassenger.Text)
        myCommand.Parameters.Add("@purpose", txtpurpose.Text)
        myCommand.Parameters.Add("@tripdate", dtptripdate1.Text)

        Dim affectedRows As Integer = cmd.ExecuteNonQuery
        If affectedRows > 0 Then
            MsgBox("Succesfully Updated")
        Else
            MsgBox("Failed to update")
        End If

        con.Close()