2015-04-15 19 views
0

我必須在C#中使用基於服務的DataBase編寫通訊錄並遇到奇怪的問題。當我撥打personTableAdapter.Update(LastDataRow)時,它會嘗試覆蓋數據庫中的身份並引發異常。使用基於服務的數據庫中的Adapter.Update()更新數據,程序試圖覆蓋身份

有沒有什麼好的方法來防止這種情況?也許迫使該計劃忽略該領域?

Here is the whole program

private DataRow LastDataRow = null; 

    private void UpdateRowToDatabase() 
    { 
     if (LastDataRow != null) 
     { 
      if (LastDataRow.RowState == DataRowState.Modified) 
      { 
       personTableAdapter.Update(LastDataRow); 
      } 
     } 
    } 

    private void regionBindingSource_PositionChanged(
     object sender, EventArgs e) 
    { 
     BindingSource thisBindingSource = 
      (BindingSource)sender; 
     DataRow ThisDataRow = 
      ((DataRowView)thisBindingSource.Current).Row; 
     if (ThisDataRow == LastDataRow) 
     { 
      throw new ApplicationException("It seems the" + 
       " PositionChanged event was fired twice for" + 
       " the same row"); 
     } 

     UpdateRowToDatabase(); 

     LastDataRow = ThisDataRow; 
    } 

    private void MainForm_FormClosed(
     object sender, FormClosedEventArgs e) 
    { 
     UpdateRowToDatabase(); 
    } 

全部異常文本:http://imgur.com/buwWo7m

+0

你可以顯示'personTableAdapter'更新查詢嗎? –

+0

我還沒有構建任何...我使用自動生成的東西。它在github上可用。 (https://github.com/Hajtosek/CSharp/blob/master/ServiceBased%20AddressBook/AddressBookV2/DataSet1.Designer.cs)這是非常複雜的事情。 – Haito

+0

請增加異常的全文 –

回答

1

從評論感動 麻煩的是在數據集生成更新查詢this._adapter.UpdateCommand.CommandText = @"UPDATE [dbo].[Person] SET [Id] = @Id,我不能準確地告訴解決您的業務規則和id代的方法。例如,您可以使用SET IDENTITY_INSERT person ON;或使用DataSet中的Person.Id列屬性來播放更新中的SET [email protected]。可能是這個thougths幫助你

相關問題