Domanda

I'm working on a monitoring program for our shipping department that will show orders that are ready to ship. I've been working with SQLDependency to do this and finally got it functioning, however, when I try to add some more specifics to my query, specifically comparing to a datetime column, the OnChange event is repeatedly triggered. When I remove the comparison, it works perfectly.

I've been through Microsoft's documentation, but I don't see anything saying this type of comparison isn't valid.

I've simplified my query down to:

SELECT [SALESLINE].[SHIPPINGDATEREQUESTED]
FROM [dbo].[SALESLINE]
WHERE [SALESLINE].[SHIPPINGDATEREQUESTED] >= '20130614'

Does anyone know what I might be doing wrong or another way to compare against a date?

I can post some of my code, but, as I said, it is working if I don't have a datetime in my WHERE clause.

È stato utile?

Soluzione

Try using a typed parameter:

WHERE [SALESLINE].[SHIPPINGDATEREQUESTED] > @someDateTime

and pass in a parameter of type DateTime.

Altri suggerimenti

Are you checking the reason that the OnChange event was fired? It's likely that your query doesn't meet some of the requirements. If your query itself is to blame, it could be that the SQLDependency subscription itself is the cause of the repeated firing, in which case the reason should confirm this. You can check this in the handler by looking at the eventargs.

Reading the rules, nothing obviously stands out. So perhaps try:

using(SqlCommand cmd = new SqlCommand("SELECT [SALESLINE].[SHIPPINGDATEREQUESTED]
FROM [dbo].[SALESLINE]
WHERE [SALESLINE].[SHIPPINGDATEREQUESTED] >= @d", conn)){
DateTime myDate = DateTime.Now;
cmd.Parameters.Add(new SqlParameter("@d", myDate));

Try creating your command parameter as follows. Make sure that dateVar is a DateTime type variable not a string.

cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = dateVar;

I hope this will help someone

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top