VB中的WHERE语句。 NET OleDB / SQL查询

时间:2014-01-25 22:50:31

标签: sql vb.net oledb oledbconnection

感谢您抽出宝贵时间来查看我的问题!我是这一切的新手,我正在努力解决这个问题,但我一直在打砖墙。我正在尝试使用String的值更新MDB文件中的表,其中在表的另一部分中满足其他条件。我意识到调用字符串是不好的做法!

我认为问题出在SQL语句的WHERE部分?

感谢任何帮助,这是代码的一部分:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Declerations For Calling on
    Dim AnimalHouse As String
    AnimalHouse = "TestText"
    Dim AddressForAssingment As Integer
    AddressForAssingment = 1
    Dim IDCheckAssignment As Integer
    IDCheckAssignment = 1

    'Connection Information 
    Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ProjectDirectory.Text)
    Dim myCommand As New OleDbCommand("INSERT INTO IOInformation SET Description= '" & AnimalHouse & "' WHERE ID_number= '" & AddressForAssingment & "' AND ID_Check= '" & IDCheckAssignment & "'")
    myCommand.Connection = myConnection
    myConnection.Open()
    myCommand.ExecuteNonQuery()
    myConnection.Close()
End Sub

2 个答案:

答案 0 :(得分:0)

INSERTUPDATE是两个不同的命令。

您的查询现在是两者的混合,您想要的可能是:

UPDATE IOInformation SET Description = .... WHERE ...

答案 1 :(得分:0)

喜欢这个。请查看Parameterized Queries

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 'Declerations For Calling on
 Dim AnimalHouse As String
 AnimalHouse = "TestText"
 Dim AddressForAssingment As Integer
 AddressForAssingment = 1
 Dim IDCheckAssignment As Integer
 IDCheckAssignment = 1

 'Connection Information 
 Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ProjectDirectory.Text)
 Dim myCommand As New OleDbCommand("Update IOInformation SET [Description] = @animalHouse, WHERE ID_number = @addrForAssn AND ID_Check = @Id))
 myCommand.Parameters.AddWithValue("@animalHouse", AnimalHouse)
 myCommand.Parameters.AddWithValue("@addrForAssn", AddressForAssingment)
 myCommand.Parameters.AddWithValue("@Id", IDCheckAssignment)
 myCommand.Connection = myConnection
 myConnection.Open()
 myCommand.ExecuteNonQuery()
 myConnection.Close()