Hi smile,
Please refer below sample.
HTML
<b>Select Database:</b>
<asp:DropDownList ID="ddlDatabases" runat="server" AutoPostBack="false">
</asp:DropDownList>
<br />
<b>Select Backup:</b>
<asp:DropDownList ID="ddlBackupfiles" runat="server" AutoPostBack="false">
</asp:DropDownList>
<br />
<asp:Button ID="btnRestore" runat="server" Text="Restore" OnClick="btnRestore_Click" />
<br />
<asp:Label ID="lblMessage" ForeColor="Red" runat="server" Text=""></asp:Label>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
private string constr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillDatabases();
ReadBackupFiles();
}
}
private void FillDatabases()
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = constr;
con.Open();
string sqlQuery = "SELECT * FROM sys.databases";
SqlCommand cmd = new SqlCommand(sqlQuery, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlDatabases.DataSource = ds.Tables[0];
ddlDatabases.DataTextField = "name";
ddlDatabases.DataValueField = "database_id";
ddlDatabases.DataBind();
}
catch (SqlException sqlException)
{
lblMessage.Text = sqlException.Message.ToString();
}
catch (Exception exception)
{
lblMessage.Text = exception.Message.ToString();
}
}
private void ReadBackupFiles()
{
try
{
if (!Directory.Exists(@"c:\SQLServerBackups\"))
{
Directory.CreateDirectory(@"c:\SQLServerBackups\");
}
string[] files = Directory.GetFiles(@"c:\SQLServerBackups\", "*.bak");
ddlBackupfiles.DataSource = files;
ddlBackupfiles.DataBind();
ddlBackupfiles.SelectedIndex = 0;
}
catch (Exception exception)
{
lblMessage.Text = exception.Message.ToString();
}
}
protected void btnRestore_Click(object sender, EventArgs e)
{
try
{
string databasName = ddlDatabases.SelectedItem.Text.ToString();
string backupName = @"C:\SQLServerBackups\IndexInternals2008.bak";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = constr;
conn.Open();
string sqlQuery = "RESTORE DATABASE " + databasName + " FROM DISK ='" + backupName + "'";
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
cmd.CommandType = CommandType.Text;
int result = cmd.ExecuteNonQuery();
conn.Close();
lblMessage.Text = "The " + databasName + " database restored with the name " + backupName + " successfully...";
}
catch (SqlException sqlException)
{
lblMessage.Text = sqlException.Message.ToString();
}
catch (Exception exception)
{
lblMessage.Text = exception.Message.ToString();
}
}
VB.Net
Private constr As String = ConfigurationManager.ConnectionStrings("connectionString").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
FillDatabases()
ReadBackupFiles()
End If
End Sub
Private Sub FillDatabases()
Try
Dim con As SqlConnection = New SqlConnection()
con.ConnectionString = constr
con.Open()
Dim sqlQuery As String = "SELECT * FROM sys.databases"
Dim cmd As SqlCommand = New SqlCommand(sqlQuery, con)
cmd.CommandType = CommandType.Text
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
ddlDatabases.DataSource = ds.Tables(0)
ddlDatabases.DataTextField = "name"
ddlDatabases.DataValueField = "database_id"
ddlDatabases.DataBind()
Catch sqlException As SqlException
lblMessage.Text = sqlException.Message.ToString()
Catch exception As Exception
lblMessage.Text = exception.Message.ToString()
End Try
End Sub
Private Sub ReadBackupFiles()
Try
If Not Directory.Exists("c:\SQLServerBackups\") Then
Directory.CreateDirectory("c:\SQLServerBackups\")
End If
Dim files As String() = Directory.GetFiles("c:\SQLServerBackups\", "*.bak")
ddlBackupfiles.DataSource = files
ddlBackupfiles.DataBind()
ddlBackupfiles.SelectedIndex = 0
Catch exception As Exception
lblMessage.Text = exception.Message.ToString()
End Try
End Sub
Protected Sub btnRestore_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim databasName As String = ddlDatabases.SelectedItem.Text.ToString()
Dim backupName As String = "C:\SQLServerBackups\IndexInternals2008.bak"
Dim conn As SqlConnection = New SqlConnection()
conn.ConnectionString = constr
conn.Open()
Dim sqlQuery As String = "RESTORE DATABASE " & databasName & " FROM DISK ='" & backupName & "'"
Dim cmd As SqlCommand = New SqlCommand(sqlQuery, conn)
cmd.CommandType = CommandType.Text
Dim result As Integer = cmd.ExecuteNonQuery()
conn.Close()
lblMessage.Text = "The " & databasName & " database restored with the name " & backupName & " successfully..."
Catch sqlException As SqlException
lblMessage.Text = sqlException.Message.ToString()
Catch exception As Exception
lblMessage.Text = exception.Message.ToString()
End Try
End Sub