ورود

View Full Version : عمل نکردن Query Notification



mtahmasebi
جمعه 09 تیر 1391, 17:59 عصر
دوستان سلام

من در نظر دارم توی اپلیکیشنم از قابلیت Query Notification استفاده کنم یه برنامه نوشتم که ظاهرا طبق اصول هست و موقع دیباگ کردن هم ایرادی گرفته نمیشه اما کار نمیکنه. در واقع تنها مشکلش اینه که موقع اضافه شدن رکورد جدید به بانک برنامه واکنشی نشون نمیده!
هرکاری کردم جواب نداد، دوستان با تجربه لطفا کمک کنند. ایراد کار کجاست؟:افسرده:

ضمنا از sql server 2008 , VS2008 استفاده می کنم.

این هم کد نوشته شده:





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Permissions;

namespace QueryNotification2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private bool CanRequestNotifications()
{
// In order to use the callback feature of the
// SqlDependency, the application must have
// the SqlClientPermission permission.
try
{
SqlClientPermission perm =
new SqlClientPermission(System.Security.Permissions.Pe rmissionState.Unrestricted);

perm.Demand();

return true;
}
catch
{
return false;
}
}


private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = CanRequestNotifications();
}

private void button1_Click(object sender, EventArgs e)
{

string connstr = "Data Source=(local);Initial Catalog=PardisDB;UID=PardisUser1;Pwd=12345678";
string myCMD = "Select * from testTBL";
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(myCMD, conn);
SqlDependency depend = new SqlDependency(cmd);
SqlDependency.Start(connstr);
depend.OnChange += new OnChangeEventHandler(depend_OnChange);
conn.Open();
SqlDataReader datareader = cmd.ExecuteReader();

while (datareader.Read())
listBox1.Items.Add(datareader[0].ToString());




}

static void depend_OnChange(object caller, SqlNotificationEventArgs e)
{
MessageBox.Show("ok");

}


}
}

tooraj_azizi_1035
یک شنبه 18 تیر 1391, 18:14 عصر
سلام
باید در رویداد depend_OnChange دیتاست را دوباره Fill کنی:


private void dependency_OnChange(
object sender, SqlNotificationEventArgs e)
{
// This event will occur on a thread pool thread.
// Updating the UI from a worker thread is not permitted.
// The following code checks to see if it is safe to
// update the UI.
ISynchronizeInvoke i = (ISynchronizeInvoke)this;

// If InvokeRequired returns True, the code
// is executing on a worker thread.
if (i.InvokeRequired)
{
// Create a delegate to perform the thread switch.
OnChangeEventHandler tempDelegate =
new OnChangeEventHandler(dependency_OnChange);

object[] args = { sender, e };

// Marshal the data from the worker thread
// to the UI thread.
i.BeginInvoke(tempDelegate, args);

return;
}

// Remove the handler, since it is only good
// for a single notification.
SqlDependency dependency =
(SqlDependency)sender;

dependency.OnChange -= dependency_OnChange;

// At this point, the code is executing on the
// UI thread, so it is safe to update the UI.
++changeCount;
label1.Text = String.Format(statusMessage, changeCount);

// Add information from the event arguments to the list box
// for debugging purposes only.
listBox1.Items.Clear();
listBox1.Items.Add("Info: " + e.Info.ToString());
listBox1.Items.Add("Source: " + e.Source.ToString());
listBox1.Items.Add("Type: " + e.Type.ToString());

// Reload the dataset that is bound to the grid.
GetData();
}




در اینجا می بینی که دوباره داده ها Fill شده اند:


private void GetData()
{
// Empty the dataset so that there is only
// one batch of data displayed.
dataToWatch.Clear();

// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;

// Create and bind the SqlDependency object
// to the command object.
SqlDependency dependency =
new SqlDependency(command);
dependency.OnChange += new
OnChangeEventHandler(dependency_OnChange);

using (SqlDataAdapter adapter =
new SqlDataAdapter(command))
{
adapter.Fill(dataToWatch, tableName);

dataGridView1.DataSource = dataToWatch;
dataGridView1.DataMember = tableName;
}
}



منبع:http://msdn.microsoft.com/en-us/library/a52dhwx7%28v=vs.80%29.aspx