views:

213

answers:

2

I have used SMO objects to generate SQL Script for database objecrs for SQL Server. This works well when the database is in the local network. But it takes a lot of time if database is in a remote server. What would be the best and fastest way of generating scripts for SQL Server objects when database is in remote server.

Thanks in advance

A: 

You can try precaching required objects to improve performance with PrefetchObjects method:

var server = new Server(new ServerConnection(connectionString));
var database = server.Databases[databaseName];

database.PrefetchObjects(typeof(Table));

If you're trying to implement anything related to database schema migration, check out Wizardby.

Anton Gogolev
I tried this:Server server = new Server(con);var database = server.Databases[vstrDatabase];database.PrefetchObjects(typeof(Table)); foreach(Table tbl in database.Tables){ sbr = new StringBuilder(); StringCollection script = tbl.Script(); foreach (string str in script) { }}tbl.Script takes plenty of time for remote server, so the whole process is taking a long time. I need to create scripts for all object. So what would be the best way.
Anshuman Roy
+1  A: 

I would use SQL Server Management Studio

From the Database item right click and select tasks, generate scripts. Many options there for you to enjoy.

(You can even have it script data in the tables.)

Hogan
I have to do this programmatically using c#.
Anshuman Roy