Search code examples
c#ado.netsql-server-cesql-server-ce-4

'System.StackOverflowException' occurred in System.Data.SqlServerCe.dll


I'm using SQL CE 4.0 and am running into a StackOverflowException when the SqlCeDataAdapter attempts to Fill the DataTable.

The code is pretty standard :

using (var cmd = new SqlCeCommand(sql, conn, tran))
using (var dt = new DataTable())
{
    using (var da = new SqlCeDataAdapter(cmd))
        da.Fill(dt);

    IList<DataRow> rows = dt.Rows.OfType<DataRow>().ToList();
    foreach (DataRow row in rows)
    {
        // Processing ...
    }
}

The sql string is in the form of :

SELECT * FROM [Table] WHERE Id IN ( ... )

The total character count is 1,068,369 due to the column value list being filled with 27,393 values of type uniqueidentifier.

Executing that query should return results with 1-for-1 rows from the database. Instead, the StackOverflowException occurs. I feel as though I am running up against some sort of SQL CE 4.0 limitation.


I've read Understanding Databases (SQL Server Compact) > Database Objects and under the Queries section it reads :

Characters in an SQL statement    |    Unlimited

1,068,369 characters is less than unlimited; PASS.


I've read DataTable Class and under the Remarks section it reads :

The maximum number of rows that a DataTable can store is 16,777,216

27,393 rows is less than 16,777,216; PASS.


Is there something else that I'm missing?


Solution

  • "The code is pretty standard" (!) - but the SQL statement is not! It is obviously an engine bug/doc error, but do not expect a fix from Microsoft. You will have to change the code to work with smaller batches of ids, resulting in a statement that is not 1 MB in size with 27.000 values.