0

请帮助我...我正在尝试从 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”

4

2 回答 2

2

从. [DEP_ID]=@DEP_ID,_ SET尝试将其设置为WHERE无论如何都已确保的值是没有意义的,因此它显然是多余的,并且不允许更新IDENTITY列。

UPDATE DEPARTMENT
SET    [DEPART] = @DEPART,
       [DEP_DSCRPTN] = @DEP_DSCRPTN
WHERE  [DEP_ID] = @DEP_ID 
于 2013-01-07T11:14:03.073 回答
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 列,那么我认为您需要更好地分析您的数据库模式。

于 2013-01-07T11:15:50.900 回答