Answered by:
Query returns System.AccessViolationException: Attempted to read or write protected memory.

Question
-
I have a query that when run from my winform application (cmd.ExecuteScalar()) or executed in Data Port Console (3rd party SQL CE manager), it throws the following error.
System.AccessViolationException: Attempted to read or write protected memory.
When I run it via SQL 2008 SSMS, no error is thrown.
Here is the query. It does return the correct value, but the error makes the app unusable.
SELECT TOP (1) archived_id FROM (SELECT TOP (1) archived_id, UTC_TIME, DATEDIFF(second, UTC_TIME, '7/12/2009 1:16:00 AM') AS time_difference FROM Archived_table WHERE (UTC_TIME <= '7/12/2009 1:16:00 AM') AND (CityId = 'cityname') ORDER BY UTC_TIME DESC UNION ALL SELECT TOP (1) archived_id, UTC_TIME, DATEDIFF(second, '7/12/2009 1:16:00 AM', UTC_TIME) AS time_difference FROM Archived_table WHERE (UTC_TIME > '7/12/2009 1:16:00 AM') AND (CityId = 'cityname') ORDER BY UTC_TIME) AS T ORDER BY time_difference
No other queries cause this issue. This code was working but I believe this started happening after I upgraded to Visual Studio 2010.
Any help would be appreciated.
Thanks
Thursday, April 29, 2010 5:25 PM
Answers
-
I limited the query to a simple select and it works fine, so there is obviously an issue with the actual SQL statement and the SQL CE that ships with VS2010. It worked before I upgraded.
- Proposed as answer by Alex Feng (SQL) Monday, May 3, 2010 8:53 AM
- Marked as answer by Alex Feng (SQL) Friday, May 21, 2010 10:22 AM
Friday, April 30, 2010 3:02 AM -
I ended up having to split up the SQL statements into two SqlCeResultSets and comparing the datediffs to retrieve the id I needed.
I am not sure what the problem is with the original query and the new VS2010/SQL CE but I have put on this band-aid for now.
- Proposed as answer by Alex Feng (SQL) Monday, May 3, 2010 8:53 AM
- Marked as answer by Alex Feng (SQL) Friday, May 21, 2010 10:22 AM
Friday, April 30, 2010 4:07 AM
All replies
-
I have confirmed this error is happening in the System.Data.SQLServerCE dll 3.5.1, runtime version 2.0.50727.
As I said before, it works with all other select/insert/update/delete statements, so I don't think it is a problem with it running on the 4.0 framework.
Any ideas?
Friday, April 30, 2010 2:48 AM -
I limited the query to a simple select and it works fine, so there is obviously an issue with the actual SQL statement and the SQL CE that ships with VS2010. It worked before I upgraded.
- Proposed as answer by Alex Feng (SQL) Monday, May 3, 2010 8:53 AM
- Marked as answer by Alex Feng (SQL) Friday, May 21, 2010 10:22 AM
Friday, April 30, 2010 3:02 AM -
I ended up having to split up the SQL statements into two SqlCeResultSets and comparing the datediffs to retrieve the id I needed.
I am not sure what the problem is with the original query and the new VS2010/SQL CE but I have put on this band-aid for now.
- Proposed as answer by Alex Feng (SQL) Monday, May 3, 2010 8:53 AM
- Marked as answer by Alex Feng (SQL) Friday, May 21, 2010 10:22 AM
Friday, April 30, 2010 4:07 AM -
Hi,
Please check this KB article: FIX: Error message when you run a .NET Framework 2.0 Remoting application: "Unhandled Exception: System.AccessViolationException".
Thanks,
Chunsong
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Monday, May 3, 2010 8:58 AM -
Feng, this is not related to the question.
http://erikej.blogspot.com Erik Ejlskov Jensen - Please mark as answer, if this was it.Monday, May 3, 2010 2:16 PM