我想在使用 SqlDataAdapter 填充数据时收听 RowChanged 事件。它有效,但是尽管数据没有改变,但事件还是引发了两次。有人可以解释一下为什么吗?我能做些什么才能只加注一次?
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);
}
}