using System;
using TableDependency.SqlClient;
using TableDependency.SqlClient.Base;
using TableDependency.SqlClient.Base.Enums;
using TableDependency.SqlClient.Base.EventArgs;
namespace SQLCDC
{
class
Program
{
static
void Main(string[] args)
{
string
connectionString = "Server=localhost;Database=Test;Integrated
Security=True;";
var
mapper = new ModelToTableMapper<People>();
mapper.AddMapping(c
=> c.name, "Name");
mapper.AddMapping(c
=> c.age, "Age");
using
(var dep = new SqlTableDependency<People>(connectionString,
"People", mapper: mapper))
{
dep.OnChanged
+= Changed;
dep.Start();
Console.ReadKey();
dep.Stop();
}
}
public
static void Changed(object sender, RecordChangedEventArgs<People> e)
{
var
changedEntity = e.Entity;
if
(e.ChangeType != ChangeType.None)
{
switch
(e.ChangeType)
{
case
ChangeType.Delete:
break;
case
ChangeType.Update:
break;
case
ChangeType.Insert:
break;
case
ChangeType.None:
Console.WriteLine("None");
break;
}
}
Console.WriteLine("DML
OPR: " + e.ChangeType);
Console.WriteLine(" Name: " + changedEntity.name);
Console.WriteLine(" Age: " + changedEntity.age);
Console.WriteLine();
}
}
public
class People
{
public
string name { get; set; }
public
string age { get; set; }
}
}
/*
use
master
select
is_broker_enabled from sys.databases where name= 'Test'
use
test
go
create
table people (
id
int identity (1,1) not null,
name
varchar(50) not null,
age
nchar(10),
primary
key (id)
)
select
* from people
insert
into people (name, age) values ('A', 45)
insert
into people (name, age) values ('B', 50)
insert
into people (name, age) values ('C', 23)
select
* from people
update
people set name='A1' where name='A'
--
truncate table people
*/
Read more...