0

I would like to use the SqlCeEngine class to build a database and populate it with tables (like this question) but using a script generated by ExportSqlCE.

I was wondering if this was possible using SqlBulkCopy? (A SQL Server CE version which has been replicated here).

Or if there is another solution to quickly populating a .sdf database file with tables and constraints (28 tables, 52 constraints, most columns cannot be null).

When my application loads I check to see if the database file exists. In the eventuality that this file is missing (or when the program is loaded for the first time after installation) I want to be able to re-generate an empty database.

I created the database using DataPort Console because as its my first real database application I wanted to design the database graphically.

Community
  • 1
  • 1
Simon Johnson
  • 97
  • 2
  • 11
  • Why not simply include an empty database file with your application, and copy it to the desired location if required? – ErikEJ Mar 12 '13 at 17:17
  • In the event the Database file is lost I want the the application to be able to maintain itself. – Simon Johnson Mar 13 '13 at 08:20
  • Yes, my proposal will allow you to do that... – ErikEJ Mar 13 '13 at 09:10
  • This does not solve my issue as later I want to have the option of altering the database. I want to have the ability to give the user an updated version of the application that may need to re-build the database. I would like the option of extracting their data, deleting the database, rebuilding with a new schema and then importing their data under the guise of "Program Initialization After Installation" so that they cannot see changes. I'd rather not have to provide a new Database (even if its empty) whenever I change things – Simon Johnson Mar 13 '13 at 10:04
  • OK, now you explain your requirements! - You should provide ALTER TABLE/CREATE TABLE scripts with the updated app, and maybe store the current database version in a database table. – ErikEJ Mar 13 '13 at 11:13
  • Just tested some create table scripts in an app to see how this can be handled. Just gonna have to populate a List of strings and then run the SqlCeCommand for each element. I hadn't considered recording database version yet so that's something I'll add to the design. – Simon Johnson Mar 13 '13 at 11:21

1 Answers1

1

Ultimately I was not able to find a solution that replicated my desired functionality and instead, following the advice of ErikEJ decided to implement a series of scripts to be run on load. The ExportSqlCE is still useful for creating scripts of an already defined SQL Server CE database.

I eventually implemented a List<string> in which I added the various scripts. This allows me the option of loading scripts if I want to update a Database or choosing hardcoded "default" scripts for the initial version of a database. I could then loop around the List elements changing the SqlCeCommand command.CommandText property to the next element in the List.

Community
  • 1
  • 1
Simon Johnson
  • 97
  • 2
  • 11