问题
Ok, Firstly Hey.
Technically i am new to this site, however i have been using it as a resource for a few years, and it is only because i am stumped that i now come in need of your aid.
I have a VB script that is Querying the Db, filling the intended boxes with the string and displaying it.
My issue is that when i try to use the Update SQL command, it completes however it does not update the DB when i manually check.
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Data.mdb';")
Dim da As New OleDbDataAdapter
Dim dt As New DataTable()
Dim cmd As New OleDbCommand
da.SelectCommand = New OleDbCommand("update FDSL set Host=@Host, Owner=@Owner where ID = @ID", con)
Dim paramID As New OleDbParameter
paramID.ParameterName() = "@ID"
paramID.Value() = Label8.Text
da.SelectCommand.Parameters.Add(paramID)
Dim paramHost As New OleDbParameter
paramHost.ParameterName() = "@Host"
paramHost.Value() = TextBox1.Text
da.SelectCommand.Parameters.Add(paramHost)
Dim paramOwn As New OleDbParameter
paramOwn.ParameterName() = "@Owner"
paramOwn.Value() = TextBox4.Text
da.SelectCommand.ExecuteNonQuery()
MessageBox.Show("Record Updated! ", "Update Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
Refresh()
con.Close()
I have tried the params using the .tostring on the end as well, but this does not work either.
----------Solved----------------
I must thank @Gord Thompson for this assistance, however i solved the issue by recoding it.
Dim str As String
str = "update FDSL set Hostname=@Hostname, Owner=@Owner where ID=@id"
Dim cmd As New OleDbCommand(str, con)
cmd.Parameters.AddWithValue("@Hostname", TextBox1.Text)
cmd.Parameters.AddWithValue("@Owner", TextBox2.Text)
cmd.Parameters.AddWithValue("@ID", textbox6.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
This now works correctly thank your for helping again!
回答1:
Jet.OLEDB
and ACE.OLEDB
ignore the names of parameters so we need to specify parameters in the exact order in which they appear in the CommandText. In your case you need to change the order in which you execute your .Parameters.Add()
blocks so you do @Host
, then@Owner
, and then @ID
.
回答2:
I must thank @Gord Thompson for hist assistance, however i solved the issue by recoding it.
Dim str As String
str = "update FDSL set Hostname=@Hostname, OS=@OS where ServerID=@id"
Dim cmd As New OleDbCommand(str, con)
cmd.Parameters.AddWithValue("@Hostname", TextBox1.Text)
cmd.Parameters.AddWithValue("@OS", TextBox2.Text)
cmd.Parameters.AddWithValue("@ID", textbox6.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
This now works correctly thank your for helping again!
来源:https://stackoverflow.com/questions/19979758/access-update-with-visual-basic-sql