Read Time1 Minute, 36 Second

SMO (Sql Server Management Object) can be used in .net application for dealing with database objects like table, stored procedure, views, and functions.

Basically you have to use “Microsoft.SqlServer.Management.Smo” namespace to use its classes in your application.

Additionally you need to use Microsoft.SqlServer.Management.Sdk.Sfc assembly.

I have used following assemblies for this sample code:

using System.Data;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using smo = Microsoft.SqlServer.Management.Smo;

Following code mentions use of SMO to copy tables and data.

/* SOURCE Database */
string myDBServer = “sourceDBServer”;
string myDBUsername = “testuser”;
string myDBPassword = “password”;
string myDBDatabase = “firstDB”;

/* DESTINATION Database */
string destServer = “destinationDBServer”;
string destLogin = “testuser”;
string destPassword = “password”;
string destDatabase = “targetDB”;

ServerConnection con = new ServerConnection(myDBServer, myDBUsername, myDBPassword);
smo.Server sqlServer = new smo.Server(con);
Database db = sqlServer.Databases[myDBDatabase];

Transfer transfer = new Transfer(db);
transfer.CopyAllUsers = true;
transfer.CreateTargetDatabase = false;
transfer.CopyAllObjects = false;
transfer.CopyAllTables = true;
transfer.CopyData = true;
transfer.Options.WithDependencies = true;
transfer.Options.DriAll = true;
transfer.Options.ContinueScriptingOnError = false;

//use following code if want to create destination databaes runtime
ServerConnection destConnection = new ServerConnection(destServer, destLogin, destPassword);
Server destServerObj = new Server(destConnection);
Database newdb = new Database(destServerObj, destDatabase);
transfer.CreateTargetDatabase = true;
transfer.CreateTargetDatabase = false;
transfer.DestinationLoginSecure = true;
transfer.DestinationServer = destServer;
transfer.DestinationLogin = destLogin;
transfer.DestinationPassword = destPassword;
transfer.DestinationDatabase = destDatabase;

Personally I think aobve programm is very usefull when we need to take backup SQL Server database.

NOTE : Also visit

If you are in need of any
Web Development feel free to Inquire us . Dhanashree Inc. Expertise in Development, Php Development,
Website designing
, Open Source customisation. Dhanashree Inc can be our offshore development company /
outsourcing web development company, hire dedicated web programmers.

Above information is for knowledge sharing if you have problem / issue / suggestion please intimate us with details for proper and prompt action.

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

Leave a Reply