• 0

BackUp Database(SQL Server 2005) using C#


Question

Hi Guys,

I am trying to backup database using C# code and i couldnt get it working. The server is SQL server 2005

Error for backup Backup failed for Server 'BDServer\\sql1'. \r\nStackTrace : at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)\r\n at BackupRestore.Program.Main(String[] args) in C:\\Documents and Settings\\tvs1\\Desktop\\BackupRestore\\BackupRestore\\Program.cs:line 84\r\nInner1 : Cannot write property CompressionOption. This property is not available on SQL Server 2005.\r\n

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using Microsoft.SqlServer.Management.Smo;

namespace BackupRestore
{
	class Program
	{
		static void Main(string[] args)
		{


string MachineName="BDServer\\sql1";

string DBName="TEST";

string backupName = "Backup";

Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(MachineName);

Database db = server.Databases[DBName];

//To Avoid TimeOut Exception

server.ConnectionContext.StatementTimeout = 60 * 60;

RecoveryModel recoverymodel = db.DatabaseOptions.RecoveryModel;

//Define a Backup object variable.

Backup backup = new Backup();

backup.CompressionOption = BackupCompressionOptions.On;

//Specify the type of backup, the description, the name, and the database to be backed up.

backup.Action = BackupActionType.Database;

backup.BackupSetDescription = "Full backup of " + DBName + ".";

backup.BackupSetName = backupName;

backup.Database = DBName;

//Declare a BackupDeviceItem

BackupDeviceItem bdi = default(BackupDeviceItem);

bdi = new BackupDeviceItem(@"L:\ACM\Temp\Vinu\" + backupName + ".bak", DeviceType.File);

//Add the device to the Backup object.

backup.Devices.Add(bdi);

//Set the Incremental property to False to specify that this is a full database backup.

backup.Incremental = false;

//Set the expiration date.

System.DateTime backupdate = new System.DateTime();

backupdate = new System.DateTime(2008, 10, 5);

backup.ExpirationDate = backupdate;

//Specify that the log must be truncated after the backup is complete.

backup.LogTruncation = BackupTruncateLogType.Truncate;

//Run SqlBackup to perform the full database backup on the instance of SQL Server.

try
{

   backup.SqlBackup(server);

}

catch (Exception ex)
{

	String str = "Error for backup " + ex.Message + Environment.NewLine;

	str += "StackTrace : " + ex.StackTrace + Environment.NewLine;

	if (ex.InnerException != null)
	{

		str += "Inner1 : " + ex.InnerException.Message + Environment.NewLine;

		if (ex.InnerException.InnerException != null)
		{

			str += "Inner2 : " + ex.InnerException.InnerException.Message + Environment.NewLine;

		}

	}



	Console.WriteLine(str);
	Console.ReadLine();

}




//Remove the backup device from the Backup object.

backup.Devices.Remove(bdi);

		}
	}
}

Help appreciated

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Grab Motorola's Razr Ultra 2025 at 15% off and ditch your slab phone by Paul Hill Motorola is currently selling the Razr Ultra 2025 on Amazon at a discount of 15%, meaning you can pick it up now for $1,099.99 instead of the usual $1,299.99. This is a notable saving on a premium foldable smartphone, but it still remains a high-end purchase. Any Prime members out there can get free overnight delivery and if you have an old device to trade in, then you can get up to $725 in Amazon.com Gift Card credit that you can use towards your upgrade. One of the main perks of this device is that it’s universally unlocked, meaning you can use it on all major US carriers. It’s powered by the Snapdragon 8 Elite and comes with 16GB RAM and 512GB storage. With these specs, this device should be able to run any apps you throw at it. The Pantone Scarab color, a soft, velvety suede-like texture, is the result of a collaboration with the Italian brand Alcantara, known for its premium materials, giving the phone a very unique look. As a flip phone, you also get an intelligent and interactive external display that lets you access apps without opening your phone. Even with this external display, Motorola says you can expect over 36 hours of battery life and when you do need to recharge, you have fast charging. The main display on this device is 7 inches and features a Super HD resolution which is 2992 x 1224, the refresh rate is 165Hz. The external display has a resolution of 1272 x 1080. This phone could be a good pick for fashion-conscious users with its unique materials. It’s also a great device for anyone after a compact form factor or who is a fan of Motorola devices. The style, compact design, external display, fast performance, and improved camera system (50MP) are all positives on this device. There’s also IP48 rating for dust and water protection, adding durability. Motorola Razr Ultra 2025: $1,099.99 (Amazon US) / MSRP $1,299.99 This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Lots of people want a world where everything is personal and nothing is ever discussed rationally. List me as one of the people who doesn't think that's a good approach.
    • I think it's great that we're learning more about the early universe through observation. Knowing that our assumptions were off is a good thing. Once space based gravitational wave detectors exist, we may be able to "see" into the period where the universe was still condensed before matter formed. That'll be cool
  • Recent Achievements

    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
    • Week One Done
      macomen earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      653
    2. 2
      ATLien_0
      253
    3. 3
      Xenon
      168
    4. 4
      neufuse
      147
    5. 5
      +FloatingFatMan
      127
  • Tell a friend

    Love Neowin? Tell a friend!