Deploying a database

K

kitemad69

How i can i deploy and attach a database from a C# project. I created
the database in sql manager now i want to deploy it using a C# project
is this possible
 
A

Alberto Poblacion

How i can i deploy and attach a database from a C# project. I created
the database in sql manager now i want to deploy it using a C# project
is this possible

Assuming that Sql Server is already installed on the computer where you
want to deploy the database, you have at least these options:

1) You can attach the database. This would require dettaching it from the
original server (or stopping the Sql Server) and copying the .mdf and .ldf
files. You would then transfer those files to the computer where you want
them deployed, and attach them to the local sql server by means of the
adequate call to the sp_attach_db stored procedure which is documented in
the Sql Server manuals. Of course, you can do this from C# by means of the
ExecuteNonQuery method of a SqlCommand.

2) If you are using Sql Server Express, and you want the database for local
use in single-user mode, you can use the "user instance" mode of sql server.
This doesn't require you to program anythig in your C# code (except copying
the mdf file), you just have to edit your connection string so that it uses
the necessary parameters for this mode of operation and contains the path to
the copied mdf.

3) You can use a backup. Extract the backup with Sql Manager, copy it to the
deployment media, and restore it with a "Restore Database..." Sql query with
adequate parameters. Of course, this query can be sent from C# with a
SqlCommand and ExecuteNonQuery.

In all three cases, you get the database on the deployment computer, but
you still have to worry about security, because the database users contained
in the database are mapped to sql logins in the original Sql Server, not in
the deployment Sql Server. You can create users and grant permissions once
again by sending the corresponding Sql calls with a SqlCommand. This is not
needed if you are using a user instance, which always maps the current user
to the dbo of the database.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top