C# でデータベース テーブルへの変更を監視するにはどうすればよいですか? SQL Server 2008 を使用しており、テーブルが変更されたときにデータを同期したいと考えています。
3553 次
2 に答える
3
私があなたを正しく理解しているなら、あなたはSqlDependencyを作成したいと思っています。
void Initialization()
{
// Create a dependency connection.
SqlDependency.Start(connectionString, queueName);
}
void SomeMethod()
{
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
SqlCommand command=new SqlCommand(
"SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers",
connection);
// Create a dependency and associate it with the SqlCommand.
SqlDependency dependency=new SqlDependency(command);
// Maintain the refence in a class member.
// Subscribe to the SqlDependency event.
dependency.OnChange+=new OnChangeEventHandler(OnDependencyChange);
// Execute the command.
command.ExecuteReader();
// Process the DataReader.
}
// Handler method
void OnDependencyChange(object sender,
SqlNotificationsEventArgs e )
{
// Handle the event (for example, invalidate this cache entry).
}
void Termination()
{
// Release the dependency
SqlDependency.Stop(connectionString, queueName);
}
于 2012-05-03T09:31:57.310 に答える
2
Microsoft Sync Frameworkを確認してください。
于 2012-11-17T11:20:01.737 に答える