开发者

Limiting amount of rows in OleDbDataAdapter query

开发者 https://www.devze.com 2023-01-05 05:39 出处:网络
I am using C# and .NET 2.0. I created an OleDbDataAdapte开发者_Go百科r to import a tab delimited file and in the SQL for one of my commands, I am adding \'LIMIT 1\' to the end of the query.

I am using C# and .NET 2.0. I created an OleDbDataAdapte开发者_Go百科r to import a tab delimited file and in the SQL for one of my commands, I am adding 'LIMIT 1' to the end of the query.

string query = string.Format("SELECT DISTINCT * 
                                FROM {0} 
                              ORDER BY ZipCode 
                              LIMIT 1", tableName);

I run the query and my program throws an exception, System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression 'ZipCode LIMIT 1'.

I am just running some tests now but I'm curious as to why it does not work or if there is an alternate way to limit the amount of rows returned. I am working with files that have tens of thousands of rows and sometimes might need to limit the amount of rows returned.


As per my comment, the answer depends on the backend DB

For SQL Server you woult use TOP

string query = string.Format("SELECT DISTINCT TOP 1 *  
                              FROM {0}  
                              ORDER BY ZipCode", tableName);

For Oracle you would need to use ROWNUM in the where clause which queries against a sub-select. Something like this (untested)

string query = string.Format("SELECT * FROM (SELECT DISTINCT *  
                              FROM {0}  
                              ORDER BY ZipCode) where ROWNUM = 1", tableName);

And so on, depending on your backend DB.

0

精彩评论

暂无评论...
验证码 换一张
取 消