SQL Compact select top 1

走远了吗. 提交于 2019-12-21 07:48:06

问题


While porting an application from SQL 2005 to SQL Server Compact Edition, I found that I need to port this command:

SELECT TOP 1 Id FROM tblJob WHERE Holder_Id IS NULL

But SQL Server Compact Edition doesn't support the TOP keyword. How can I port this command?


回答1:


SELECT TOP(1) Id 
FROM tblJob 
WHERE Holder_Id IS NULL

Need the brackets as far as I know.

reference: http://technet.microsoft.com/en-us/library/bb686896.aspx

addition: likewise, only for version 3.5 onwards




回答2:


This is slightly orthogonal to your question.

SQL Server Compact Edition actually doesn't perform very well with SQL queries. You get much better performance by opening tables directly. In .NET, you do this by setting the command object's CommandText property to the table name, and the CommandType property to CommandType.TableDirect.

If you want to filter the results, you will need an index on the table on the column(s) you want to filter by. Specify the index to use by setting the IndexName property and use SetRange to set the filter.

You can then read as many or as few records as you like.




回答3:


I've used Fill method of SqlCEDataAdapter. You can do:

DbDataAdapter.Fill (DataSet, Int32, Int32, String) Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names. Supported by the .NET Compact Framework.

http://msdn.microsoft.com/en-ie/library/system.data.common.dbdataadapter.fill(v=VS.80).aspx




回答4:


Looks like it can't be done in compact. You have to read all the jobs, or make a SqlReader, and just read the first one.




回答5:


Well found a reason. Management studio carries and uses it's own version od SQL Server Compact. See more in http://en.wikipedia.org/wiki/SQL_Server_Compact.

SQL Server Management Studio 2005 can read and modify CE 3.0 and 3.1 database files (with the latest service pack), but the SQL Server Management Studio 2008 from the "Katmai" 2008 CTP release (or later) is required to read version 3.5 files.

The RTM of SQL Server Management Studio 2008 and Microsoft Visual Studio Express 2008 SP1 can create, modify and query CE 3.5 SP1 database files.



来源:https://stackoverflow.com/questions/138419/sql-compact-select-top-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!