Click here to Skip to main content
15,917,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i have a database named mydb in SQLserver 2005
and i have created an application in .net which uses this db

now i want to take the backup of this database but from front end means clicking on button which is on the form...

(front end means from visual studio 2008) i don't want to go in SQL server and then take backup.....i want to take it from application
Posted

Take a look here for the same questions and answers on that:
http://stackoverflow.com/questions/728658/creating-sql-server-backup-file-bak-with-c-to-any-location[^]

Good luck!
 
Share this answer
 
Solved.......
i have solved my problem....by the following code...

create a new web application in VS and make a folder in root directory named "mybackups"
now

copy and paste the bellow code in .aspx file named "Default.aspx"

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
    Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Database backup and restore tool</title>
    <style>
        td, p
        {
            font: normal 11px arial;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="lblMessage" runat="Server" EnableViewState="False" ForeColor="Red"></asp:Label>
    <table style="border: 1px solid #cccccc; border-collapse: collapse">
        <tr>
            <td style="vertical-align: top; border-right: 1px solid #cccccc; padding-left: 15px;
                padding-top: 10px">
                <b>Database Tables</b>
            </td>
            <td style="vertical-align: top; border-right: 1px solid #cccccc; padding-left: 15px;
                padding-top: 10px">
                <b>Download Backups</b>
            </td>
            <td style="vertical-align: top; padding-left: 15px; padding-top: 10px">
                <b>Upload Backup</b>
            </td>
        </tr>
        <tr>
            <td style="vertical-align: top; border-right: 1px solid #cccccc; padding: 15px;">
                <table>
                    <tr>
                        <td>
                            <asp:ListBox ID="ListBox1" runat="Server" DataTextField="table_name" DataValueField="table_name"
                                Style="width: 200px;" Rows="10"></asp:ListBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span onclick="return confirm('Are you sure to backup selected table?')">
                                <asp:Button ID="btnBackup" runat="Server" Text="Backup Table" OnClick="BackUpNow"
                                    CssClass="smallbutton2" /></span> <span onclick="return confirm('Are you sure to restore selected table?')">
                                <asp:Button ID="btnRestore" runat="Server" Text="Restore Table" OnClick="RestoreNow"
                                            CssClass="smallbutton2" /></span>
                        </td>
                    </tr>
                </table>
            </td>
            <td style="vertical-align: top; border-right: 1px solid #cccccc; padding: 15px;">
                <asp:Literal ID="backupList" runat="server"></asp:Literal>
            </td>
            <td style="vertical-align: top; padding: 15px;">
                <asp:Literal ID="uploadMessage" runat="server"></asp:Literal>
                Â
                <br />
                <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                <asp:Button ID="btnUpload" runat="server" OnClick="uploadBackup" Text="Upload Backup File" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>




and also copy and paste the bellow code in .cs file named "Default.aspx.cs"


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
string connectionstring = @"Data Source=futureistic;Initial Catalog=chest;User ID=brijendra;Password=password_1";
string backupfolder = "~/mybackups";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDatabaseTables();
}
listBackups();
}

private void PopulateDatabaseTables()
{
string tableName = string.Empty;
string sql = "SELECT *, name AS table_name " + " FROM sys.tables WHERE Type = 'U' ORDER BY table_name";
using (SqlConnection conn = new SqlConnection(connectionstring))
{
using (DataTable table = new DataTable())
{
using (SqlDataAdapter dAd = new SqlDataAdapter(sql, conn)) { dAd.Fill(table); }
ListBox1.DataSource = table;
ListBox1.DataBind();
}
}
}

protected void BackUpNow(object sender, EventArgs e)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{

string tableName = ListBox1.Items[i].ToString();
using (DataSet dSetBackup = new DataSet())
{
using (SqlConnection conn = new SqlConnection(connectionstring))
{
using (SqlDataAdapter dAd = new SqlDataAdapter("select * from " + tableName, conn))
{
dAd.Fill(dSetBackup, tableName);
}
}
if (!Directory.Exists(Server.MapPath(backupfolder)))
{
Directory.CreateDirectory(Server.MapPath(backupfolder));
}
dSetBackup.WriteXml(Server.MapPath(backupfolder + "/" + tableName + ".xml"), XmlWriteMode.WriteSchema);
lblMessage.Text = "Backup for table " + tableName + " successful!
";
}
listBackups();
}
}

protected void RestoreNow(object sender, EventArgs e)
{
string tableName = ListBox1.SelectedValue;
if (File.Exists(Server.MapPath(backupfolder + "/" + tableName + ".xml")))
{
string xmlFile = Server.MapPath(backupfolder + "/" + tableName + ".xml");

DataSet ds = new DataSet();

ds.ReadXml(xmlFile);
DataTable dt = new DataTable();
dt = ds.Tables[0];

SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
SqlCommand comm = new SqlCommand("truncate table " + tableName, conn);
comm.ExecuteNonQuery();
conn.Close();

SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionstring, SqlBulkCopyOptions.KeepIdentity);
bulkCopy.DestinationTableName = tableName;
bulkCopy.WriteToServer(dt);

lblMessage.Text += "Restore of table " + tableName + " successful!
";
}
else
{
lblMessage.Text += "Table " + tableName + " has not been backed up yet, so cannot be restored.
";

}
}

protected void listBackups()
{
string fPath = Server.MapPath(backupfolder);
if (Directory.Exists(fPath))
{
string filelinks = "";
DirectoryInfo dir = new DirectoryInfo(fPath);
FileInfo[] files = dir.GetFiles("*.xml");
foreach (FileInfo file in files)
{
filelinks += "" + file.Name + " }
if (filelinks == "")
{
backupList.Text = "No backups have been made yet.";
}
else
{
backupList.Text = "Click to download a backupfile below
(right click and select 'save target as')

}
}
else
{
backupList.Text = "No backups have been made yet.";
}
}

protected void uploadBackup(object sender, EventArgs e)
{
HttpPostedFile myFile = FileUpload1.PostedFile;

if (myFile != null)
{
string backupPath = Server.MapPath(backupfolder) + "\\" + Path.GetFileName(myFile.FileName);
myFile.SaveAs(backupPath);
lblMessage.Text = "Backup file " + Path.GetFileName(myFile.FileName) + " uploaded successfully";
listBackups();
}
else
{
lblMessage.Text = "No File was uploaded";
}
}
}
 
Share this answer
 
v3

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