Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/* I use below code but .net 2.0 Compiler give me some error about Microsoft.SqlServer.Management.Smo

please help me if u have better way to create backup and restore it

(I add this Assambly To project

Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Management.Sdk.Sfc.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.SmoExtended.dll

)
*/
using System;
using System.Data.SqlClient;

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace YourNamespace
{
/// <summary>
/// Database access generics
/// </summary>
public static class DBAccess
{
#region Constants
/// <summary>
/// Connection string to DB
/// </summary>
public static readonly string ConnectionString = @"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=AudioMaster;Integrated Security=True";
#endregion

#region Public Methods
/// <summary>
/// Backup a whole database to the specified file.
/// </summary>
/// <remarks>
/// The database must not be in use when backing up
/// The folder holding the file must have appropriate permissions given
/// </remarks>
/// <param name="backUpFile">Full path to file to hold the backup</param>
public static void BackupDatabase(string backUpFile)
{
ServerConnection con = new ServerConnection(@"xxxxx\SQLEXPRESS");
Server server = new Server(con);
Backup source = new Backup();
source.Action = BackupActionType.Database;
source.Database = "MyDataBaseName";
BackupDeviceItem destination = new BackupDeviceItem(backUpFile, DeviceType.File);
source.Devices.Add(destination);
source.SqlBackup(server);
con.Disconnect();
}
/// <summary>
/// Restore a whole database from a backup file.
/// </summary>
/// <remarks>
/// The database must not be in use when backing up
/// The folder holding the file must have appropriate permissions given
/// </remarks>
/// <param name="backUpFile">Full path to file to holding the backup</param>
public static void RestoreDatabase(string backUpFile)
{
ServerConnection con = new ServerConnection(@"xxxxx\SQLEXPRESS");
Server server = new Server(con);
Restore destination = new Restore();
destination.Action = RestoreActionType.Database;
destination.Database = "MyDataBaseName"; ;
BackupDeviceItem source = new BackupDeviceItem(backUpFile, DeviceType.File);
destination.Devices.Add(source);
destination.ReplaceDatabase = true;
destination.SqlRestore(server);
}
#endregion
}
}
Posted
Comments
[no name] 22-Jul-13 16:32pm    
"give me some error", the phrase "some error" is neither helpful nor specific in describing your problem.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900