views:

1052

answers:

3

I would like to distribute my C# application along with SQL Server Express 2005 and the apps corresponding database. When installed SQL Server Express 2005 needs to have my sample database automatically loaded with data, not just table structures, when it is installed. How can I accomplish this.

Is there a way to do this with the Visual Studio Click Once technology? I have VS 2008.

+1  A: 

If you can run a SQL script that creates the tables, couldn't that same script also INSERT rows?

Wayne
+1  A: 

If the base sample data is the same for each installation, you can include a detached MDF/LDF and attach it at the time of the installation to the new SQL Express Instance. Or you can restore a BAK file of the database after installation...?

Let me know if this helps!
JFV

JFV
Is there a way to restore a BAK file in SQL Server using code from a C# app or do you have to do it manually.
J3r3myK
Here is a good place to refer to using VB.NET: http://www.shabdar.org/sql-server-backup-utility.htmlYou shouldn't have any issues porting this over to C# to automatically restore your BAK file for your Sample DB. I'm not as familiar with C#, or I'd convert it for you. Thanks!
JFV
+3  A: 

There are several ways you can do this - as JFV mentioned, you can ship a MDF/LDF database and re-attach it after installation.

Or you could wrap up your database creation scripts into a .NET project and/or stand-alone EXE with something like Red-Gate's SQL Packager.

Or yet again, on SQL Server Express, you could simply add your MDF file to a directory and use the "attach local database file" mechanism in ADO.NET to connect to your database file. Your connection string would look something like:

Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

Your choice! :-)

Cheers, Marc

marc_s