i have person table in sql server with fields id and Name
and csharp statement that work well.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SQLNotifications
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string connectionstring = @"Server=EEPERSIAN-PC\SQLEXPRESS;Database=Chatter;User ID=sa;pwd=1";
delegate void GridDelegate(DataTable table);
SqlDependency dep;
private void Form1_Load(object sender, EventArgs e)
{
SqlDependency.Start(connectionstring);
UpdateGrid();
}
private void UpdateGrid()
{
string sql = "select Name from dbo.person";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
dep = new SqlDependency(cmd);
dep.OnChange += dep_OnChange;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
}
}
}
dataGridView1.Invoke(
(GridDelegate)delegate(DataTable table)
{
dataGridView1.DataSource = table;
}
, dt);
}
private void dep_OnChange(object sender, SqlNotificationEventArgs e)
{
MessageBox.Show("Insert Accourd");
UpdateGrid();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SqlDependency.Stop(connectionstring);
}
}
}
now , i have 2 problem :
1- i cant understant this section of code
dataGridView1.Invoke(
(GridDelegate)delegate(DataTable table)
{
dataGridView1.DataSource = table;
}
, dt);
2- i want monitor multi table instead of one table . How i can do this ?
Please help me to solve this problem.