Answer the question
In order to leave comments, you need to log in
How to track changes in MS SQL 2008 DB in C# through Service Broker?
Greetings. I can't figure out how to track changes in the database.
Available:
public Main_Form()
{
InitializeComponent();
SqlDependency.Stop(connectionString);
SqlDependency.Start(connectionString);
ScoutingSQL();
DataGridView_Load();
}
// создаем dependency и подписываем его на событие
// которое должно вызвать Service Broker
private void ScoutingSQL()
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT sentDate, verificationDate, verifiedTo", connection);
SqlDependecy dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
}
// метод, вызванный событием
void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
DataGridView_Load();
}
// Загружаем данные из sql бд в наш грид
public void DataGridView_Load()
{
while(dataGridView1.Rows.Count > 0)
for (int i = 0; i < dataGridView1.Rows[i]);
{
dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
}
string tableName = "[staff_106].[dbo].[staff]"
string querry = ("SELECT * FROM" + tableName + "");
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(querry, connection);
// и тд... просто делаем Fill из dataSet, заполняя грид.
}
ALTER DATABASE [Database_name] SET ENABLE_BROKER;
SqlDependency.Start(connectionString, queueName);
// Execute the command.
using (SqlDataReader reader = command.ExecuteReader())
{
// Process the DataReader.
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question