Iam using Sqldependency to monitor my table changes in the database the problem is Sqldependency not work when query contains left join to another table. After research I find that on of the sqldependency limitation its not work with left join. how I can handle this issue ?
public JsonResult Get()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerConnection"].ConnectionString))
{
connection.Open();
//NOTE: [dbo].[CutomerInfo] WITH [dbo] IS MANDATORY WHILE USING SQL DEPENDENCY
using (SqlCommand command = new SqlCommand(@"SELECT [CusId],[CusName], LocationName FROM [dbo].[CutomerInfo]
left join [dbo].[CustomerLocations] on [dbo].[CustomerLocations].[CustomerId] =[dbo].[CutomerInfo].CusId
WHERE [Status] <> 0", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
//COMMAND EXECUTION IS MANDATORY
SqlDataReader reader = command.ExecuteReader();
var listCus = reader.Cast<IDataRecord>()
.Select(x => new
{
CusId = (int)x["CusId"],
CusName = (string)x["CusName"],
LocationName = (x["LocationName"]==DBNull.Value)?"":(string)x["LocationName"]
}).ToList();
return Json(new { listCus = listCus }, JsonRequestBehavior.AllowGet);
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
// do something
}
I know that join and inner join work with Sql dependency but I need left join