Friday, February 24, 2012

How to copy structure of database online

Hi!

I have to add a new functionnality in our Web application but i have

no idea how to do it. What i have to do is to allow user to create a

copy of the BD currently use and to save it on a new name. What i need

is only the structure not the data. I need to copy all table, view,

function and stored proc. I have no idea where to start. I found on

this forum post where it says to detach then copy and the attach the BD

but i can't do to this because i don't want other users on our web

application to lost the connection with the DB.

So was is the best way to do this ?

Thanks !!

you can leverage SMO namespaces/classes to accomplish this from a webapp. There are several ways to actually do this, but I think SMO is the "optimal"/recommend approach here. Again, all you need are the scripts, not the data.

http://www.sqlteam.com/item.asp?ItemID=23185

Derek

|||Thanks !! This is exactly what i was looking for. I have another question. When i iterate for all my tables for example i can have the script of the table using myTable.script(). What i was wondering is how can i send this script to SQL Server ? First i probably need to create my new database but do i have to do this using script ?|||

Ok i think i've figure out how to send my script but once again i have another question. Right now what i'm doing is this:

For Each tmpTable In MainDB.Tables

NewDB.ExecuteNonQuery(tmpTable .Script)

Next

This seems to created my tables. Our DB contain over 700 tables,over 500 stored proc and over 200 views. So what i was wondering, is it a good solution to do this like i'm doing right now or will it be better to put all the script in one collection string and then execute on big script ?

Thanks !

No comments:

Post a Comment