Quantcast
Channel: Tech Insight ! » Database
Viewing all articles
Browse latest Browse all 2

Copy Tables and Data using SMO (Sql Server Management Object) in asp.net

$
0
0

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);
newdb.Create();
transfer.CreateTargetDatabase = true;
*/
transfer.CreateTargetDatabase = false;
transfer.DestinationLoginSecure = true;
transfer.DestinationServer = destServer;
transfer.DestinationLogin = destLogin;
transfer.DestinationPassword = destPassword;
transfer.DestinationDatabase = destDatabase;
transfer.ScriptTransfer();
transfer.TransferData();

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


NOTE : Also visit www.dhanashree.com


If you are in need of any
Web Development feel free to Inquire us . Dhanashree Inc. Expertise in Asp.net 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.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images