By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

Deploying a database

P: n/a
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
Sep 30 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<ki*******@gmail.comwrote in message
news:14**********************************@u65g2000 hsc.googlegroups.com...
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.

Sep 30 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.