Question

I want to add rows in a DataSet, but only a specific number of rows (in order to avoid the "Too many fields defined." exception). This is what I have tried so far:

OleDbCommand command = new OleDbCommand(@"SELECT TOP 100 FROM [" + SheetName + "]", connection);

Exception: The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

OleDbCommand command = new OleDbCommand(@"SELECT * FROM [" + SheetName + "] LIMIT 100", connection);

Exception: Syntax error in FROM clause.

I also tried this that I got from another site

OleDbCommand command = new OleDbCommand(@"SELECT TOP 100 * FROM [" + SheetName + "]", connection);

Exception: Too many fields defined.

I have no idea what else to try, any suggestions?

Was it helpful?

Solution

You can specify how many rows you would like the Fill() method to retrieve. Here's its signature from MSDN:

public int Fill(
    int startRecord,
    int maxRecords,
    params DataTable[] dataTables
)

http://msdn.microsoft.com/en-us/library/0z5wy74x.aspx

OTHER TIPS

Well, I believe you cannot solve the problem that that are too many fields by limiting the rows.

But limiting the fields can be done. The problem with your first try

TOP 100 FROM 

is that the syntax is not valid. If you want to limit the fields, you need to explicitly use the columnnames like this

SELECT TOP 100 ColumnName1, ColumnName2, ColumnName3 from ....

In general, I would recommend never to use select * but always precisely the columns you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top