Click here to Skip to main content
Licence CPOL
First Posted 28 May 2008
Views 23,685
Downloads 550
Bookmarked 24 times

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

By | 28 May 2008 | Article
How to use SMO to copy database schema and data using C#.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This article shows how to use SQL Server Management Objects (SMO) and C# to Script to copy database schema with or without data.

Using the code

I have created a class DBHelper which is used to hold SQL Server instance information such as server name and database name, and the CopyDatabase method is used to copy the database. 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 SQL Server using Microsoft SQL Server Management Objects (SMO). Now I am in the process of writing a library which will interact with SQL Server for various parameters such as instance information, network information, backup and restore script, etc., and will publish it 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

Software Developer (Senior)

India India

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
BugHow to copy complete database with all table constraints( FKs, PKs ..) Pinmembermyssa271:39 27 Sep '11  
Generalerror occured..:( Pinmemberurooj_mahmood20:54 25 May '10  
Questionhow to copy live data base to my local system?? Pinmemberurooj_mahmood19:57 25 May '10  
QuestionExclude System Objects Pinmemberstixoffire16:34 11 Sep '09  
QuestionHow do I do this for Access database Pinmemberwkusmider18:00 14 Jun '09  
QuestionVisual Studio 2005? Pinmemberfreddy123459:50 20 Mar '09  
GeneralThanks PinmemberAdam Crawford23:42 12 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 28 May 2008
Article Copyright 2008 by SmartCodar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid