2011-12-20 49 views
0

表的主鍵是使用Is Identity自動生成的,我想根據主鍵進行更新。下面的代碼肯定有問題,所以我希望有人爲我修改它。提前致謝。如何更新自動生成主鍵的數據

 Try 
     Dim qry As String 
     qry = "Update StudentDetails.Programmes set [email protected],[email protected],[email protected],[email protected] where ID=ID" 
      cmd = New SqlCommand(qry, cn) 
      cmd.Parameters.Add(New SqlParameter("@Programme", txtProgramme.Text)) 
      cmd.Parameters.Add(New SqlParameter("@Form", txtForm.Text)) 
      cmd.Parameters.Add(New SqlParameter("@AcademicYear", txtAcademicYear.Text)) 
      cmd.Parameters.Add(New SqlParameter("@Class", txtClass.Text)) 
      cmd.ExecuteNonQuery() 
      MessageBox.Show("Record succesfully updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information) 
      showgrid() 
     End If 
     txtProgramme.Focus() 
    Catch ex As Exception 
    End Try 

'PS:下面的代碼顯示瞭如何插入數據。數據庫中的Identity是否設置爲Yes。作品完美。我想糾正的代碼是上面的更新命令。

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 
    Try 
     cmd = New SqlCommand("Insert into StudentDetails.programmes(Programme,Form,AcademicYear,Class) values(@Programme,@Form,@AcademicYear,@Class)", cn) 

     cmd.Parameters.AddWithValue("@Programme", txtProgramme.Text) 
     cmd.Parameters.AddWithValue("@Form", txtForm.Text) 
     cmd.Parameters.AddWithValue("@AcademicYear", txtAcademicYear.Text) 
     cmd.Parameters.AddWithValue("@Class", txtClass.Text) 
     cmd.ExecuteNonQuery() 
     MessageBox.Show("Record successfully saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information) 
+2

你向我們展示了更新命令,但如果是您的插入命令,爲什麼你不知道的PK?順便說一句,使用參數來防止SQL注入。 – 2011-12-20 21:52:09

+0

+1使用參數!之間如果你有一個獨特的'ID'列。你爲什麼要生成主鍵? – King 2011-12-20 21:57:17

+0

是的,我剛纔要這樣做。 – Akaglo 2011-12-20 21:59:31

回答

0

你不告訴它什麼ID匹配。

& 「 '其中ID = ID」

& 「 '其中ID ='」 & txtID.Text & 「'」

,並按照Tim的建議,以及。

+0

請自動插入ID,表單上沒有像txtID.Text這樣的控件。我的意思是txtID.Text在窗體上根本不存在。 – Akaglo 2011-12-20 22:42:05

+0

如果不知道要更新的記錄的ID,則無法運行此UPDATE!您的INSERT應該返回您在UPDATE中使用的Output參數中的新ID。您需要在更新中創建一個ID參數。這是一個非常非常基本的概念,我建議你按照@Tim Schmelter在他的回答中發佈的鏈接進行操作。 – Jim 2011-12-23 16:26:34

2

下面是有關如何使用ADO.NET檢索新的標識值的不言自明的例子:

Using con = New SqlConnection(My.Settings.CC_ConnectionString) 
    Dim newID As Int32 
    Using insertCommand = New SqlCommand("INSERT INTO Test(Value)VALUES(@Value);SELECT CAST(scope_identity() AS int)", con) 
     insertCommand.Parameters.AddWithValue("@Value", "Value1") 
     con.Open() 
     newID = DirectCast(insertCommand.ExecuteScalar, Int32) 
    End Using 

    If newID <> 0 Then 
     Using updateCommand = New SqlCommand("UPDATE TEST SET Value='Value1.1' WHERE [email protected]", con) 
      updateCommand.Parameters.AddWithValue("@idTest", newID) 
      If con.State <> ConnectionState.Open Then con.Open() 
      Dim updatedRecordCount = updateCommand.ExecuteNonQuery 
     End Using 
    End If 
End Using 

的2個重要的部分是:

SELECT CAST(scope_identity() AS int) 

將返回新的標識值

DirectCast(insertCommand.ExecuteScalar, Int32) 

如果插入了新行,它將返回新的Identity列值,0表示開啓故障。

使用參數將阻止SQL-Injection

+1

你最終解釋了自我解釋。 :-) – LarsTech 2011-12-20 22:20:37

+0

哦,請我仍然沒有得到你的代碼的含義。試着爲我編輯我的。 – Akaglo 2011-12-20 22:38:44

+0

@Akaglo:你不明白哪一部分?你的插入現在工作嗎?我想我已經添加了所有你需要的工作,所以也許你需要投入一些時間來鑽取我答案中提供的鏈接。 – 2011-12-20 22:46:23