Wednesday, March 28, 2012

script to take backup of a database

Hi,

I am trying to create a script that takes backup of a sql database. The script is failing with message "backup failed with message..."

Option Explicit

Dim server

Dim backup

Set server = CreateObject("Microsoft.SQLServer.Management.SMO.Server")

Set backup = CreateObject("Microsoft.SQLServer.Management.SMO.Backup")

backup.Action = 0

backup.Database = "test_old"

backup.SqlBackup(server)

I think this is failing because i did not specify the backup file name. Can u please let me know how can i specify it?

Also it would be great if you can point me to some relevant documentation which shows how to use SMO with vb script.

Regards

Aseem Bansal

You can't use SMO with VBScript. (Well, you can, but you can't use it to communicate with anything but the default server on your local machine.)

Books Online has good examples how to backup a database.

|||

I do not have one in vbscript, but here is one in C#...

using System;

using System.Data;

using System.Collections;

using Microsoft.SqlServer.Management.Common;

using Microsoft.SqlServer.Management.Smo;

class Program

{

static void Main(string[] args)

{

BackupDeviceItem bdi =

new BackupDeviceItem("AdventureWorks.bak", DeviceType.File);

Backup bu = new Backup( );

bu.Database = "AdventureWorks";

bu.Devices.Add(bdi);

bu.Initialize = true;

// add percent complete and complete event handlers

bu.PercentComplete +=

new PercentCompleteEventHandler(Backup_PercentComplete);

bu.Complete +=new ServerMessageEventHandler(Backup_Complete);

Server server = new Server("localhost");

bu.SqlBackup(server);

Console.WriteLine(Environment.NewLine + "Press any key to continue.");

Console.ReadKey( );

}

protected static void Backup_PercentComplete(

object sender, PercentCompleteEventArgs e)

{

Console.WriteLine(e.Percent + "% processed.");

}

protected static void Backup_Complete(object sender, ServerMessageEventArgs e)

{

Console.WriteLine(Environment.NewLine + e.ToString( ));

}

}

|||

Can you do this in a windows form application please..? Seems like it doesn't work...I don't know.. If can please let me know..thank you...darshaka..



sql

No comments:

Post a Comment