Click here to Skip to main content
6,629,885 members and growing! (19,889 online)
Email Password   helpLost your password?
Languages » C# » General     Advanced License: The Code Project Open License (CPOL)

Copy Database Schema and Data Using SQL Server Management Objects (SMO)

By SmartCodar

Article Shows How to use SMO to Copy Database Schema and data using C#
C# (C# 1.0, C# 2.0, C# 3.0), SQL, Windows (WinXP, Win2003, Vista), Visual Studio (VS2008), ADO.NET, WinForms, Dev
Posted:28 May 2008
Views:11,607
Bookmarked:16 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.80 Rating: 2.57 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4
1 vote, 20.0%
5

Introduction

This article show how to use SQL Server Management Objects (SMO), C# to Script, Copy Database Schema with or without data .

Using the code

I have created a class DBHelper which is used to hold the SQL Server Instance Information such as Server Name, Database name and having CopyDatabase method used to copy the dastabase. Please make sure that you have SQL Server 2005 and MS SQL Server Management Objects Installed on your Computer

           
public static void CopyDatabase(bool bCopyData)

{
//Set Source SQL Server Instance Information
Server server = new Server(DBHelper.SourceSQLServer); 

//Set Source Database Name [Database to Copy]
Database database = server.Databases[DBHelper.SourceDatabase]; 

//Set Transfer Class Source Database
Transfer transfer = new Transfer(database);

//Yes I want to Copy All the Database Objects
transfer.CopyAllObjects = true;

//In case if the Destination Database / Objects Exists Drop them First
transfer.DropDestinationObjectsFirst = true;

//Copy Database Schema
transfer.CopySchema = true;

//Copy Database Data Get Value from bCopyData Parameter
transfer.CopyData = bCopyData;

//Set Destination SQL Server Instance Name
transfer.DestinationServer = DBHelper.DestinationSQLServer;

//Create The Database in Destination Server
transfer.CreateTargetDatabase = true; 

//Set Destination Database Name
Database ddatabase = new Database(server, DBHelper.DestinationDatabase);

//Create Empty Database at Destination
ddatabase.Create();

//Set Destination Database Name
transfer.DestinationDatabase = DBHelper.DestinationDatabase;

//Include If Not Exists Clause in the Script
transfer.Options.IncludeIfNotExists = true; 

//Start Transfer
transfer.TransferData();

//Release Server variable
server = null;
}
        

Points of Interest

Yes we can play with the SQL Server using Microsoft SQL Server Management Objects (SMO). Now I am in process of writing a library which will interact with SQL server for various parameters such as Instance information, Network Information, Backup, Restore and Script etc. and will publish the same as soon as possible

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SmartCodar


Member
I am a Senior Application Developer with over 7.5 years of experience in Information Technology. I have worked as a lead developer in several windows (desktop) and web based Client/Server projects. my role mainly includes coding in VB 6.0, Vb.Net, C# language in Net framework. My current working domain is desktop, web development in VB.Net, C# using Windows API, Remoting, Cryptographic API and Software Security. The domain of projects I have has worked on so far includes PMO, Insurance Industry, Energy Management, Software Security and other client specific projects.
Occupation: Software Developer (Senior)
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
QuestionExclude System Objects Pinmemberstixoffire17:34 11 Sep '09  
GeneralHow do I do this for Access database Pinmemberwkusmider19:00 14 Jun '09  
GeneralVisual Studio 2005? Pinmemberfreddy1234510:50 20 Mar '09  
GeneralThanks PinmemberAdam Crawford0:42 13 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 May 2008
Editor:
Copyright 2008 by SmartCodar
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project