2013-01-07 79 views
0

請幫助我...我試圖從VB.net更新我的數據庫。它顯示錯誤。我的代碼如下....使用標識列更新數據庫時出現錯誤

Try 
      getConnect() 

      Dim strSQL As String 
      strSQL = " UPDATE DEPARTMENT SET [DEP_ID][email protected]_ID,[DEPART][email protected], [DEP_DSCRPTN][email protected]_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 

和錯誤是:

ERROR: Cannot update identity column 'DEP_ID'

+0

錯誤消息已清除,請移除Set DEP_ID = @DEP_ID部分。 – Steve

回答

0

錯誤消息已清除,請移除Set DEP_ID = @DEP_ID部分。

Try 
    getConnect() 

    Dim strSQL As String 
    strSQL = "UPDATE DEPARTMENT SET [DEPART][email protected]," + 
      "[DEP_DSCRPTN][email protected]_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 

如果您發現自己需要更改標識列,那麼我認爲您需要更好地分析您的數據庫模式。

+0

SET IDENTITY_INSERT對更新沒有任何影響,從查詢中可以明顯看出,無論如何它們都不需要更新它。 「SET」與列中的值相同。 –

+0

Thnx史蒂夫和馬丁史密斯..非常感謝你... Itz正常工作..... Thnx爲您的快速響應.... :) – Thanzeem

2

SET刪除[DEP_ID][email protected]_ID,。無論如何嘗試將其設置爲已由WHERE確保的值是沒有意義的,因此它顯然是多餘的,並且不允許更新IDENTITY列。

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