1

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.

2 Answers 2

3

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

I have to do this programmatically using c#.
0

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.

3 Comments

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.
The help for PrefetchObjects states This enables collections to be fully populated with objects by making one network trip to the instance of SQL Server. -- have you ever watched this one roundtrip using Profiler? INSANE!
It generates thousands of roundtrip pairs USE [YourDbName] + EXEC something -- this still takes ages against a server connected via internet. I today tried database.PrefetchObjects(typeof(Statistic)) against a server inside the LAN and it did not come to an end within minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.