13 September 2009 ~ 4 Comments

SQLCE Insert Multiple Values

If you are a newbie at Windows Mobile development and you need to create an application which is using SQLCE database, you might have bumped into this problem. Inserting multiple records into SQLCE database is quite different from doing the same thing on full blown SQL Server.

If you are like me, a developer who likes to be in control of it’s source code you are most likely using INSERT INTO statements to add new records to your database. Doing that on a SQL Server is not a new thing and it is a perfectly normal code to insert new records into your tables. Running this query on a SQL Server would get you to three new rows in a table:

Or you might have been using this variant:

SQL Server 2008 made it even easyer and all you had to do is run a query similar to this one:

And whichever syntax you might have been using would work. But, SQLCE is not like your regular SQL Server and running this kind of a query against SQLCE would inevitably throw an Exception like similar to this one:

And all of a sudden nothing makes sense. Your query is correct but SqlCeCommand is refusing to run this query on a SQLCE database!

Solution is quite simple and there are couple of them. First solution is my choice because I like writing my SQL queries. What you need to do is to create SQL INSERT INTO statement and run it against your SQLCE database. Than repeat it as many times as you need to insert all of your values. Something like this:

While this is OK for inserting fewer values, good practice to follow when inserting hundreds or thousand records is to use parametrized queries using SqlCeCommand. The code to this would be similar to this one

Both of these methods work just fine when executed against SQLCE database but as I mentioned it earlier, it might be reasonable to use the second example when inserting large sets of data as performance of this example is quite better than of the first example.

4 Responses to “SQLCE Insert Multiple Values”

  1. Alex 14 July 2010 at 13:57 Permalink

    This is not correct. It is not possible to insert multiple rows by simply adding parameters. Try that, and you’ll see an error with “Too many parameters…”. It’s better to remove that part of code, since it confuses people with no reason.

  2. prideaux 14 October 2010 at 22:54 Permalink

    Beginner’s mistake or what? as the previous poster has said, you are just updating parameters without doing anything, then when you execute only 1 row will be inserted.

  3. Eddie 10 October 2011 at 11:52 Permalink

    Just spent a good 10 minutes setting up that second method. Got this

    The SqlCeParameter with this name is already contained by this SqlCeParameterCollection.

    I really should have read the comments first.

  4. tobias 24 July 2012 at 11:59 Permalink

    This example is incorrect!
    Do *not* misuse the parameters as row’s.

    If this works in one case, there’s no guarantee that it would work on other!

    SQLServerCE does not support inserting multiple rows.
    See http://social.msdn.microsoft.com/Forums/en/sqlce/thread/18a6e510-ac7d-486b-8445-0da6fc47a488


Leave a Reply