使用Identity Column更新数据库时显示错误

时间:2013-01-07 11:11:54

标签: vb.net sql-server-2005

请帮助我......我正在尝试从VB.net更新我的数据库。它显示错误。我的代码如下......

Try
            getConnect()

            Dim strSQL As String
            strSQL = " UPDATE DEPARTMENT SET [DEP_ID]=@DEP_ID,[DEPART]=@DEPART,    [DEP_DSCRPTN]=@DEP_DSCRPTN WHERE [DEP_ID] = @DEP_ID"
            Dim cmd As New SqlCommand(strSQL, Conn)
            cmd.Parameters.AddWithValue("@DEP_ID", CInt(Me.DEPID.Text))
            cmd.Parameters.AddWithValue("@DEPART", SqlDbType.VarChar).Value =     CMBDEPT.Text
            cmd.Parameters.AddWithValue("@DEP_DSCRPTN", SqlDbType.VarChar).Value =     TXTDESC.Text
            Conn.Open()
            cmd.ExecuteNonQuery()
            MsgBox("Update Complete!", MsgBoxStyle.Information, "Update")
        Catch ex As Exception
            MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Update")
        Finally
            Conn.Close()
            BTNCLEAR.PerformClick()
        End Try

错误是:

  

错误:无法更新标识列'DEP_ID'

2 个答案:

答案 0 :(得分:2)

[DEP_ID]=@DEP_ID,移除SET。尝试将其设置为WHERE已经确保的值是没有意义的,因此它显然是多余的,不允许更新IDENTITY列。

UPDATE DEPARTMENT
SET    [DEPART] = @DEPART,
       [DEP_DSCRPTN] = @DEP_DSCRPTN
WHERE  [DEP_ID] = @DEP_ID 

答案 1 :(得分:0)

错误消息已清除,请删除Set DEP_ID = @DEP_ID部分。

Try
    getConnect()

    Dim strSQL As String
    strSQL = "UPDATE DEPARTMENT SET [DEPART]=@DEPART," + 
             "[DEP_DSCRPTN]=@DEP_DSCRPTN WHERE [DEP_ID] = @DEP_ID"
    Dim cmd As New SqlCommand(strSQL, Conn)
    cmd.Parameters.AddWithValue("@DEP_ID", CInt(Me.DEPID.Text))
    cmd.Parameters.AddWithValue("@DEPART", SqlDbType.VarChar).Value =     CMBDEPT.Text
    cmd.Parameters.AddWithValue("@DEP_DSCRPTN", SqlDbType.VarChar).Value =     TXTDESC.Text
    Conn.Open()
    cmd.ExecuteNonQuery()
    MsgBox("Update Complete!", MsgBoxStyle.Information, "Update")
Catch ex As Exception
    MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Update")
Finally
    Conn.Close()
    BTNCLEAR.PerformClick()
End Try

如果您发现自己需要更改Identity列,那么我认为您需要更好地分析数据库架构。