Fabio Milheiro
Fabio Milheiro

Reputation: 8474

How to deploy a database programatically with C#

I have a database project in my solution and the requirement is simple (not so sure about the implementation).

Is there a way to deploy the database project programatically?

PS: I don't mean attach an MDF file. I mean deploy the database to a SQL Server instance.

Upvotes: 2

Views: 1458

Answers (3)

Felice Pollano
Felice Pollano

Reputation: 33262

You can add the script for create the database as an "Embedded Resource" then execute it when needed with the standard ExecuteNonQuery(...). But you probably need to upgrade it in the future so the idea is, a part the first drop, have some table somewhere keeping the schema version, then have multiple embedded resource contatining script to jump to one version to the next one. When the service start check the version against the expected one, if not the same launch the needed script(s) to reach the current version.

Upvotes: 3

Splatbang
Splatbang

Reputation: 776

Create the database SQL however you like and include it in your project. Then simply execute the SQL script against the server.

I use this when I include SQLite databases in my project.

Upvotes: 0

Related Questions