Tuesday, March 27, 2012

Backup SQL Database in asp.net

Hi ,

Anyone knows ,How to backup sql Database in asp.net .and I need to get a .bak file through asp.net.

Pls help me.

Tamil

Assuming that the client has the required permissions to perform a backup (database permissions and permissions on the directory where you're saving the file), then you can run the [BACKUP DATABASE] command via the SQLCommand object.

Set the command type to Text, provide the appropriate SQL, and executeNonQuery. There's an MSDN example here (search the page for BACKUP) :http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.deletecommand.aspx

But note that if you have a large-ish database, you may want to up the timeout property to ensure the backup completes.

|||

Hi I got the backup database locally.But now i need to get backup for remote server.

Here is my code.

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;

using s= SQLDMO;

publicpartialclassDefault3 : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

Panel1.Visible =false;

Label3.Visible =false;

if (!IsPostBack)

{

SQLDMO.SQLServer oSQLServer =new SQLDMO.SQLServer();

SQLDMO.Restore oRestore =new SQLDMO.Restore();

SQLDMO.Database oDatabase =new SQLDMO.Database();

oSQLServer.Connect("COMP11\\SQLEXPRESS","sa","123");

foreach (SQLDMO.Database dbin oSQLServer.Databases)

{

drpdb.Items.Add(db.Name);

}

}

}

protectedvoid Butback_Click(object sender,EventArgs e)

{

Microsoft.SqlServer.Management.Common.ServerConnection serverConnection =new Microsoft.SqlServer.Management.Common.ServerConnection("COMP11\\SQLEXPRESS");

Server s;

s =newServer(serverConnection);Database db;

db = s.Databases[drpdb.SelectedItem.Text];

int rec;

rec =Convert.ToInt32(db.DatabaseOptions.RecoveryModel);

Backup sbackup =newBackup();

sbackup.Action =BackupActionType.Database;

sbackup.BackupSetDescription ="Full Backup";

sbackup.BackupSetName ="Database Backup";

sbackup.Database = drpdb.SelectedItem.Text;

BackupDeviceItem dbi;

string path = drpdb.SelectedItem.Text +".bak";

string checkpath ="D:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Backup\\" + path;if (File.Exists(checkpath)==true)

{

Panel1.Visible =true;

Label2.Text ="";Label3.Visible =false;

}

else

{

Label3.Visible =true;

Panel1.Visible =false;dbi =newBackupDeviceItem(path,DeviceType.File);

sbackup.Devices.Add(dbi);

sbackup.Incremental =false;

sbackup.LogTruncation =BackupTruncateLogType.Truncate;

sbackup.SqlBackup(s);

Label3.Text ="Full Backup complete.";

sbackup.Devices.Remove(dbi);

}

}

protectedvoid Button1_Click(object sender,EventArgs e)

{

Microsoft.SqlServer.Management.Common.ServerConnection serverConnection =new Microsoft.SqlServer.Management.Common.ServerConnection("COMP11\\SQLEXPRESS");

Server s;

s =newServer(serverConnection);Database db;

db = s.Databases[drpdb.SelectedItem.Text];

int rec;

rec =Convert.ToInt32(db.DatabaseOptions.RecoveryModel);

Backup sbackup =newBackup();

sbackup.Action =BackupActionType.Database;

sbackup.BackupSetDescription ="Full Backup";

sbackup.BackupSetName ="Database Backup";

sbackup.Database = drpdb.SelectedItem.Text;

BackupDeviceItem dbi;

string path = drpdb.SelectedItem.Text +".bak";

string checkpath ="D:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Backup\\" + path;

File.Delete(checkpath);

dbi =newBackupDeviceItem(path,DeviceType.File);

sbackup.Devices.Add(dbi);

sbackup.Incremental =false;sbackup.LogTruncation =BackupTruncateLogType.Truncate;

sbackup.SqlBackup(s);

Label3.Visible =true;

Label3.Text = drpdb.SelectedItem.Text +"Backup complete.";

sbackup.Devices.Remove(dbi);

}

protectedvoid Button2_Click(object sender,EventArgs e)

{

Panel1.Visible =true;Label2.Text ="Selected " + drpdb.SelectedItem.Text +" Database fully not Completed!";

}

}

Please help me!

Tamil

|||

Hi

Anyoneknows

I need to get Backup a database from Remote Server.

Tamil

|||

Hi,

check this

http://www.codeplex.com/SqlWebAdmin

|||

hi Mehedi Hasan

I opened that URL.but i didnt understand ,Is it match my requirement?I need to get a backup for SQL Database from remote Server.I already done for local backup.But i need to get a backup for SQL Databases from REmote Server.

Please help me with example.

Tamil

|||

hi friends,,

I got it how to backup database in remote Server.We need to give login secure false then it connects the other server.

Tamil

No comments:

Post a Comment