Saqué un ejemplo del this MSDN page y lo he usado casi textualmente. Cuando se ejecuta, el código se compila correctamente, pero changeCount
aumenta infinitamente si ha habido o no un cambio en los datos devueltos. Cuando se produce un cambio realmente dataGridView1
refleja el cambio correctamente. ¿Por qué mi SqlDependency
parece que está disparando en un bucle a pesar de que aparentemente no ha habido cambios?El uso de SqlDependency da como resultado actualizaciones constantes
Aquí está la fuente:
#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
#endregion
namespace PreAllocation_Check
{
public partial class Form1 : Form
{
int changeCount = 0;
const string tableName = "MoxyPosition";
const string statusMessage = "Last: {0} - {1} changes.";
DataSet dataToWatch = null;
SqlConnection MoxyConn = null;
SqlCommand SQLComm = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (CanRequestNotifications())
{
SqlDependency.Start(GetConnectionString());
if (MoxyConn == null)
MoxyConn = new SqlConnection(GetConnectionString());
if (SQLComm == null)
{
SQLComm = new SqlCommand(GetSQL(), MoxyConn);
SqlParameter prm = new SqlParameter("@Quantity", SqlDbType.Int);
prm.Direction = ParameterDirection.Input;
prm.DbType = DbType.Int32;
prm.Value = 100;
SQLComm.Parameters.Add(prm);
}
if (dataToWatch == null)
dataToWatch = new DataSet();
GetData();
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SqlDependency.Stop(GetConnectionString());
if (MoxyConn != null)
MoxyConn.Close();
}
private bool CanRequestNotifications()
{
try
{
SqlClientPermission SQLPerm = new SqlClientPermission(PermissionState.Unrestricted);
SQLPerm.Demand();
return true;
}
catch
{
return false;
}
}
private string GetConnectionString()
{
return "server=***;database=***;user id=***;password=***";
}
private void GetData()
{
dataToWatch.Clear();
SQLComm.Notification = null;
SqlDependency SQLDep = new SqlDependency(SQLComm);
SQLDep.OnChange += new OnChangeEventHandler(SQLDep_OnChange);
using (SqlDataAdapter adapter = new SqlDataAdapter(SQLComm))
{
adapter.Fill(dataToWatch, tableName);
dataGridView1.DataSource = dataToWatch;
dataGridView1.DataMember = tableName;
}
}
private string GetSQL()
{
return "SELECT PortID, CONVERT(money, SUM(PreAllocPos), 1) AS PreAllocation, CONVERT(money, SUM(AllocPos), 1) AS Allocation, CONVERT(money, SUM(PreAllocPos) - SUM(AllocPos), 1) AS PreLessAlloc " +
"FROM MoxyPosition " +
"WHERE CONVERT(money, PreAllocPos, 1) <> CONVERT(money, AllocPos, 1) " +
"GROUP BY PortID " +
"ORDER BY PortID ASC;";
}
void SQLDep_OnChange(object sender, SqlNotificationEventArgs e)
{
ISynchronizeInvoke i = (ISynchronizeInvoke)this;
if (i.InvokeRequired)
{
OnChangeEventHandler tempDelegate = new OnChangeEventHandler(SQLDep_OnChange);
object[] args = { sender, e };
i.BeginInvoke(tempDelegate, args);
return;
}
SqlDependency SQLDep = (SqlDependency)sender;
SQLDep.OnChange -= SQLDep_OnChange;
changeCount++;
DateTime LastRefresh = System.DateTime.Now;
label1.Text = String.Format(statusMessage, LastRefresh.TimeOfDay, changeCount);
GetData();
}
}
}
Editar: Vale la pena señalar que la base de datos que desea ejecutar esto en contra de la actualidad no han permitido que el Service Broker, y así poner a prueba mi código I copia de seguridad de mi meta la base de datos y la restauró con un nuevo nombre, luego ejecutó ALTER DATABASE my_db_name SET ENABLE_BROKER
en su contra. Todas mis pruebas han estado en esta base de datos alternativa, lo que significa que soy el único usuario en ella.
Muchas gracias! Esta respuesta me liberó de buscar e intentar por ahora unos dos días. –