Vb.net 如何使用SQL update更新access中的特定行?在我当前的代码中,我不断得到错误数据类型不匹配

Vb.net 如何使用SQL update更新access中的特定行?在我当前的代码中,我不断得到错误数据类型不匹配,vb.net,Vb.net,在我当前的代码中,我不断遇到错误数据类型不匹配的问题。在添加SQLWHERE语句以选择特定id之前,大多数错误都能解决 Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click 'tells your program the data source provider = "Provider=Micros

在我当前的代码中,我不断遇到错误数据类型不匹配的问题。在添加SQLWHERE语句以选择特定id之前,大多数错误都能解决

 Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
    'tells your program the data source
    provider = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" 'refrences to the provider
    datafile = "C:\Users\Abdulaleem\Documents\Books.accdb" 'refers to the location of the file
    connectstring = provider & datafile ' combines the 2 variables
    myconnect.ConnectionString = connectstring ' this class allows you to connect to the database
    myconnect.Open() ' opens the databse
    Dim sqlupdate As String
    sqlupdate = "Update BooksTbl  set BookName='" & TitleTEXT.Text & "' , [BookAuthor]='" & AuthorTEXT.Text & "',[Genre] ='" & GenreTEXT.Text & "',[IsAvailable] = " & status & ",[DatePublished] = '" & DateTimeCHANGER.Text & "' , [PictureLocation] = '" & PiclocTEXT.Text & "' WHERE [ID] = '" & idshower.Text & "'"
    Dim cmd As OleDbCommand = New OleDbCommand(sqlupdate, myconnect) 'creating new database object 
    Try
        cmd.ExecuteNonQuery() ' executes our sql query
        cmd.Dispose() 'deleting from ram to stop interferences
        myconnect.Close() 'closes database and connection
        TitleTEXT.Clear()
        AuthorTEXT.Clear()
        GenreTEXT.Clear()
    Catch ex As Exception ' if the programs crashes then it will be caught here and put into the ex variable
        MsgBox(ex.Message) ' shows what error occured
        myconnect.Close()
    End Try
    myconnect.Close()

假设[ID]列为整数,请尝试将其从以下位置更改为:

…其中[ID]=“&idshower.Text&”

…其中[ID]=“&idshower.Text

在access中,尝试将数字与字符串进行比较会导致消息“条件表达式中的数据类型不匹配”

注意:除非您确定idshower.text实际上包含一个数字,否则最好在执行查询之前检查该数字,否则access会认为您试图使用一个参数


另外:您可能希望研究参数化查询,尽管这些查询通常会产生更多的代码行,但可以保护您免受SQL注入攻击等攻击,并可以为您执行一些基本类型转换。

[IsAvailable]=“&status.ToString可能?已尝试,但不起作用。谢谢你的尝试啊,好吧,只是一个想法。错误发生在哪一行?错误发生在我添加时:WHERE[ID]='“&idshower.Text&'”,这在使用SQL参数而不是为SQL串接字符串时不会发生。
WHERE[ID]='“&idshower.Text&'”
强制ID为文本,勾号不是通用的SQL分隔符,它们指示文本。如果ID是int/PK AI,它可能会被stringAh卡住-比我快一点-我在看字符串,它看起来是错的,但找不到哪里。@AbdulaleemSaleh如果这个答案令人满意,你能将它标记为接受以结束问题吗?