当我的数据表UserSubsidiaryFunction
中有任何更改(插入,更新或删除)时,我将使用以下代码获取所有表:
using (SqlConnection connection = new SqlConnection(connectionString))
{
//using SqlTableDependency
using (var tableDependency = new SqlTableDependency<UserSubsidiaryFunction>(connectionString, "UserSubsidiaryFunction"))
{
tableDependency.OnChanged += TableDependency_Changed;
tableDependency.OnError += TableDependency_OnError;
tableDependency.Start();
Console.WriteLine("Waiting for receiving notifications . . .");
Console.WriteLine("Press a key to stop");
Console.ReadKey();
tableDependency.Stop();
}
}
和:
static void TableDependency_Changed(object sender, RecordChangedEventArgs<UserSubsidiaryFunction> e)
{
Console.WriteLine(Environment.NewLine);
if (e.ChangeType != ChangeType.None)
{
var changedEntity = e.Entity;
Console.WriteLine($"DML operation : {e.ChangeType}");
Console.WriteLine($"pk_UserSubsidiaryFunction new value : {e.Entity.fk_Subsidiary}");
Console.WriteLine($"fk_User : {changedEntity.fk_User}");
Console.WriteLine($"fk_Subsidiary : {changedEntity.fk_Subsidiary}");
Console.WriteLine(Environment.NewLine);
}
}
但是,如果有任何更改,我只想获取被修改的列名。 sqlTableDependency
有可能吗?如果不是,是否可以使用SqlDependency
?怎么样?谢谢