我在更新记录时遇到错误,它在更新语句中显示错误"语法错误"

时间:2015-07-24 15:15:04

标签: vb.net updates phoenix

Private Sub btn_Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Try
        If imgName <> "" Then
            Dim fs As FileStream
            fs = New FileStream(imgName, FileMode.Open, FileAccess.Read)
            Dim picByte As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
            fs.Close()

            cn.Open()
            Dim strSQL As String

            strSQL = "update student SET roll no=@rollno,name=@name,address=@add,birthdate=@bdate,year=@year,standard=@std,[Image]=@Img WHERE  Register No=@regno "


            Dim imgParam As New OleDbParameter()

            imgParam.OleDbType = OleDbType.Binary
            imgParam.ParameterName = "Img"
            imgParam.Value = picByte

            cmd = New OleDbCommand(strSQL, cn)

            cmd.Parameters.AddWithValue("@regno", txt_Regno.Text)
            cmd.Parameters.AddWithValue("@rollno", cbo_rollno.Text)
            cmd.Parameters.AddWithValue("@name", txt_name.Text)
            cmd.Parameters.AddWithValue("@add", txt_address.Text)
            cmd.Parameters.AddWithValue("@bdate", txt_bdate.Text)
            cmd.Parameters.AddWithValue("@year", txt_year.Text)
            cmd.Parameters.AddWithValue("@std", txt_std.Text)
            cmd.Parameters.Add(imgParam)
            cmd.ExecuteNonQuery()

            MessageBox.Show("Record successfully Updated.")

            cmd.Dispose()
            'cn.Close()
            cn.Dispose()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        If (cn.State = ConnectionState.Open) Then cn.Close()
    End Try


    FillCombo()

End Sub

1 个答案:

答案 0 :(得分:0)

字段名称不能包含空格,除非您通过[]保护它们:

UPDATE student 
SET    [roll no] = @rollno, -- Here
       name = @name,
       address = @add,
       birthdate = @bdate,
       year = @year,
       standard = @std,
       [Image] = @Img 
WHERE  [Register No] = @regno -- And here