BackUp And Restore Database Program
By rajesh
By rajesh
Solution
expore ->Add refrences ->
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Dts.Design
Microsoft.SQLServer.ManagedDTS
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.SqlEnum
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
Microsoft.SqlServer.Server;
using
Microsoft.SqlServer.Management.Smo;
using
Microsoft.SqlServer.Management;
using
Microsoft.SqlServer.Management.Common;
using
System.IO;
namespace
Backup_And_Restore
{
public partial class Form1 : Form
{
DataTable
dt;
private
static Server
srvSql;
public
Form1()
{
InitializeComponent();
}
private
void Form1_Load(object
sender, EventArgs e)
{
GetAllServer();
}
private
void button1_Click(object
sender, EventArgs e)
{
// Create
Connection And Get All Database Names
try
{
if
(comboBox1.SelectedItem != null &&
comboBox1.SelectedItem.ToString() != "")
{
ServerConnection
ser_Cn = new ServerConnection(comboBox1.SelectedItem.ToString());
ser_Cn.LoginSecure = false;
ser_Cn.Login =
textBox1.Text;
ser_Cn.Password =
textBox2.Text;
srvSql = new Server(ser_Cn);
MessageBox.Show("Connection Created Successfully With DataBase");
foreach
(Database dbServer in
srvSql.Databases)
{
comboBox2.Items.Add(dbServer.Name);
}
}
else
{
MessageBox.Show("Please select a server first", "Server Not Selected", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private
void button2_Click(object
sender, EventArgs e)
{
// Create
Backup Button
try
{
if
(srvSql != null)
{
SaveFileDialog
sfd = new SaveFileDialog();
sfd.Title = "BackUp To...";
sfd.Filter = "Backup File|*.bak";
sfd.FileName = "Backup_" +
comboBox2.SelectedItem.ToString() + "_"
+ DateTime.Now.ToLongDateString()
+ ".bak";
if
(sfd.ShowDialog() == DialogResult.OK)
{
Backup bkpDatabase = new Backup();
bkpDatabase.Action = BackupActionType.Database;
bkpDatabase.Database =
comboBox2.SelectedItem.ToString();
BackupDeviceItem bkpDevice = new
BackupDeviceItem(sfd.FileName, DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.SqlBackup(srvSql);
MessageBox.Show("DataBase
Backup Successfull");
}
}
else
{
MessageBox.Show("A connection to a SQL server was not
established.", "Not Connected to
Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private
void button3_Click(object
sender, EventArgs e)
{
try
{
//Restore
Backup
if
(srvSql != null)
{
OpenFileDialog
ofd = new OpenFileDialog();
ofd.Title = "BackUp To...";
ofd.Filter = "Backup File|*.bak";
if
(ofd.ShowDialog() == DialogResult.OK)
{
Restore rstDatabase = new Restore();
rstDatabase.Action = RestoreActionType.Database;
rstDatabase.Database =
comboBox2.SelectedItem.ToString();
BackupDeviceItem bkpDevice = new
BackupDeviceItem(ofd.FileName, DeviceType.File);
rstDatabase.Devices.Add(bkpDevice);
rstDatabase.ReplaceDatabase = true;
rstDatabase.SqlRestore(srvSql);
MessageBox.Show("DataBase
Restored Successfully");
}
}
else
{
MessageBox.Show("A connection to a SQL server was not
established.", "Not Connected to
Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
public void GetAllServer()
{
try
{
//Getting
a List of Available SQL Servers
dt = SmoApplication.EnumAvailableSqlServers(false);
if
(dt.Rows.Count > 0)
{
foreach
(DataRow dr in
dt.Rows)
{
comboBox1.Items.Add(dr["Name"]);
}
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
No comments:
Post a Comment