问题
I create table using sql developer
create table tablenodes
(
nodeNo int ,
nodeName varchar2(50),
centerX int ,
centerY int,
radius number(7,2),
fileNo int
)
And I want to update all fields in this table, so I wrote the following code:
Friend Function UpdateNodeToTable(ByVal FN As Integer, ByVal nd As classNode) As Boolean
Try
Dim con As New OracleConnection
con.ConnectionString = "Persist Security Info=False;User ID=manal;password=manal;Data Source=xe"
con.Open()
Dim cmd As New OracleCommand
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.CommandText = "update tablenodes set nodeName=@NodeName, centerX = @NodeCenterX," & _
"centerY= @NodeCenterY , radius= @NodeRadius where nodeNo= @nodeNum and fileno= @FileNum"
cmd.Parameters.Add("@NodeNum", OracleDbType.Int32).Value = nd.pID
cmd.Parameters.Add("@NodeName", OracleDbType.Varchar2).Value = nd.pName
cmd.Parameters.Add("@NodeCenterX", OracleDbType.Int32).Value = nd.pCenter.X
cmd.Parameters.Add("@NodeCenterY", OracleDbType.Int32).Value = nd.pCenter.Y
cmd.Parameters.Add("@NodeRadius", OracleDbType.Double).Value = nd.pRadius
cmd.Parameters.Add("@FileNum", OracleDbType.Int32).Value = FN
Dim success As Boolean
If cmd.ExecuteNonQuery() = 1 Then
success = True
Else
success = False
End If
cmd.Dispose()
con.Close()
con.Dispose()
Return success
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
I have a problem in updating statements, can't write it correctly, every time I try to edit it, gives me different error (ora-01036 illegal variable name/number, missing expression, invalid identifier).
回答1:
The bind variables in you UPDATE statement should be prefixed by a colon, not the @ symbol
cmd.CommandText = "update tablenodes set nodeName=:NodeName, centerX = :NodeCenterX," & _
"centerY= :NodeCenterY , radius= :NodeRadius where nodeNo= :nodeNum and fileno= :FileNum"
And there would be no prefix what you're setting the parameters
cmd.Parameters.Add("NodeNum", OracleDbType.Int32).Value = nd.pID
cmd.Parameters.Add("NodeName", OracleDbType.Varchar2).Value = nd.pName
cmd.Parameters.Add("NodeCenterX", OracleDbType.Int32).Value = nd.pCenter.X
cmd.Parameters.Add("NodeCenterY", OracleDbType.Int32).Value = nd.pCenter.Y
cmd.Parameters.Add("NodeRadius", OracleDbType.Double).Value = nd.pRadius
cmd.Parameters.Add("FileNum", OracleDbType.Int32).Value = FN
来源:https://stackoverflow.com/questions/9832456/update-statment-using-vb-net-to-update-all-fields