2012-07-24 56 views
0

我想在使用SqlDataAdapter填充數據時監聽RowChanged事件。儘管事實上數據沒有改變,但它仍然有效,但事件再次發生了兩次。有人可以解釋我爲什麼嗎?我能做些什麼來提高一次?DataTable OnRowChanged事件引發兩次?

class Program 
{ 
    static void Main(string[] args) 
    { 
     string connectionString = "myConnectionString"; 

     var dataTable = new DataTable(); 

     dataTable.RowChanged += new DataRowChangeEventHandler(OnDataTableRowChanged); 

     using (var sqlCon = new SqlConnection(connectionString)) 
     { 
      SqlDataAdapter adapter = new SqlDataAdapter(); 

      adapter.SelectCommand = new SqlCommand("select * from TableX", sqlCon); 

      adapter.Fill(dataTable); 
     } 

     dataTable.RowChanged -= new DataRowChangeEventHandler(OnDataTableRowChanged); 
    } 

    static void OnDataTableRowChanged(object sender, DataRowChangeEventArgs e) 
    { 
     var table = (DataTable)sender; 

     Console.WriteLine("line: " + e.Row[0] + ", RowCount: " + table.Rows.Count); 
    } 
} 

回答

1

對於COMMIT,它會觸發兩次ADD一次。

檢查e.Action它會給你它被解僱的動作