SMO Sample: Transfer

Purpose of sample: Transfer a database; all objects and data

Server tgt = new Server(".");

// Setup source connection (in this sample source is .\inst1 and target is '.' (the default instance)
Server svr = new Server(@".\inst1");
Database db = svr.Databases["testdb"];

// Setup transfer
Transfer t = new Transfer(db);
t.CopyAllObjects = true;
t.DropDestinationObjectsFirst = true;
t.CopySchema = true;
t.CopyData = true;
t.DestinationServer = ".";
t.DestinationDatabase = "testdb";
t.Options.IncludeIfNotExists = true;

// Do the work
t.TransferData(); // Or use ScriptTransfer() if you need to capture the script (without data)

Disclaimer: this sample doesn't handle exceptions and may not function as expected. Use at own risk. It is good practice to test an application before using it in production.