Pages

Wednesday, October 7, 2009

Restore .bak file sql express 2005 C# .Net By Praveen P.R

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Windows.Forms;
public void RestoreDatabase(string databaseName,string backupFileName,String serverName)
  {
  try
  {
  ServerConnection connection = new ServerConnection(serverName);//".\sqlexpress"
  Server server = new Server(connection);

  Restore restore = new Restore();
  restore.Database = databaseName;//

 //backupFileName e.g database.bak
  restore.Devices.AddDevice(backupFileName, DeviceType.File);
  restore.ReplaceDatabase = true;
  restore.ReplaceDatabase = true;
  restore.PercentCompleteNotification = 10;
  server.KillAllProcesses(databaseName);
  restore.Wait();
  restore.SqlRestore(server);
  MessageBox.Show("Database Restore success....", "Restore success", MessageBoxButtons.OK, MessageBoxIcon.None);
  }
  catch (Exception ex)
  {
  System.Windows.Forms.MessageBox.Show(ex.InnerException.ToString());
  }
  }

http://www.prpraveen.blogspot.com

No comments: