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);
}
}