Copying database schema and data using C sharp

To copy the database structure and data content from one database to another, you can use C# codes as follows:

class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Attempting connection to database…”);
// Connect to Server
ServerConnection _con = new ServerConnection();

Server server = new Server(“VM0019\\SQLEXPRESS”);

// Get the Database to Transfer
Database db = server.Databases[“gices”];

// Setup transfer
// I want to copy all objects
// both Data and Schema
Transfer t = new Transfer(db);
t.CopyAllObjects = true;
t.DropDestinationObjectsFirst = true;
t.CopySchema = true;
t.CopyData = true;
t.DestinationServer = “VM0019\\SQLEXPRESS”;
t.DestinationDatabase = “testcopy”;
t.Options.IncludeIfNotExists = true;

// Transfer Schema and Data
t.TransferData();

// Kill It
server = null;
Console.WriteLine(“Closing database connection now…Program terminated”);
}
}

You need to first create a Console Application project in Visual Studio and add reference to a couple of libraries before attempting to run the codes.

Microsoft guide to transferring data:

Transfer Schema and Data from One Database to Another

comments powered by Disqus