Update statment using vb.net to update all fields

家住魔仙堡 提交于 2021-01-29 17:34:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!