How can I provide users with database backup and recovery functions for a system developed using Asp.net?

Source: Internet
Author: User

Http://topic.csdn.net/t/20030526/20/1837137.html

 

It's a tough question. I don't know how to implement it?

In fact, I want users to learn about the backup of SQL Server (the SQL Server database is used in the background. But they are afraid that they do not want to, so they want to use their own programs to implement.

 

 

# 1st floor score: 20Reply: 20:45:11

SQL = "backup database northwind to disk = 'C: \ northwind. Bak 'with init"

 

# Second floor score: 50Reply: 20:47:45

Using sqldmo;

Protected backup2class backup = new backup2class ();
Private void startbackup_click (Object sender, system. eventargs E)
{
String servername;
String login;
String password;
String databasename;
String serverpathname;
If (session ["personnelid"] = NULL)
{
Label1.text = "Please log on to the system again! ";
Return;
}
If (checktext (). length! = 0)
{
Label1.text = checktext () + "cannot be blank! ";
Return;
}
Servername = servername. Text. Trim ();
Login = loginname. Text. Trim ();
Password = password. Text. Trim () + "";
Databasename = databasename. Text. Trim ();
Serverpathname = serverpathname. Text. Trim ();

Sqldmosqlserver. loginsecure = false;
Try
{
Sqldmosqlserver. Connect (servername, login, password );

Databases = sqldmosqlserver. databases;
Database2 = (database2) databases. Item (databasename, null );

Backup. percentcompletenotification = 1;

Backup. Database = database2.name;
Backup. Files = @ serverpathname;

Backup. sqlbackup (sqldmosqlserver );
Label1.text = "complete backup! ";
}
Catch (exception ex)
{
Label1.text = ex. Message. tostring ();
}
Catch
{
Label1.text = "backup failed! ";
}
Finally
{
Sqldmoclear ();
}
}
Private void sqldmoclear ()
{
Sqldmosqlserver. Disconnect ();
Sqldmosqlserver. Close ();
Backup = NULL;
Sqldmosqlserver = NULL;
}
Private string checktext ()
{
String strstate = "";
If (servername. Text. Trim (). Length = 0)
{
Strstate = "server name ";
Return strstate;
}
If (loginname. Text. Trim (). Length = 0)
{
Strstate = "Administrators ";
Return strstate;
}
If (databasename. Text. Trim (). Length = 0)
{
Strstate = "Database Name ";
Return strstate;
}
If (serverpathname. Text. Trim (). Length = 0)
{
Strstate = "[server path] backup ";
Return strstate;
}
Return strstate;
}

 

#8 floor score: 30Reply: 09:03:29

You can call the backup stored procedure:
Create procedure gy_dbbak
@ Bakequip int, -- backup device: disk & tape

@ Bakpath varchar (50), -- backup file name with full path
@ Baktype int, -- full backup & Incremental Backup

@ Baklog int, -- '0' back up logs

@ Bakdb int, -- '0' back up the database

@ Kind varchar (7) -- backup or recovery

@ Retmsg varchar (20) Output -- return information
As
Declare @ devname_data varchar (50)
Declare @ devname_log varchar (50)
Declare @ db_path varchar (100)
Declare @ log_path varchar (100)

Declare @ RC int

Select @ db_path = @ bakpath + '. dat'
Select @ log_path = @ bakpath + 'Log. dat'
Select @ rc = 0
Select @ devname_data = 'dali ', @ devname_log = 'dalilog'

DBCC checkdb (database name)
/*************************************** ********************
** Create Backup and Restore Devices
**************************************** ********************/
If @ rc = 0
Begin

Exec sp_addumpdevice 'disk', @ devname_data, @ db_path

Exec sp_addumpdevice 'disk', @ devname_log, @ log_path
Select @ RC =error
If @ RC <> 0
Begin
Exec sp_dropdevice @ devname_data
Exec sp_dropdevice @ devname_log
Select @ rc =-1000
Return @ RC
End
End

If @ kind = 'backup'
Begin
If @ bakequip = 0
Begin
If @ baktype = 0
Begin
If @ bakdb = 0
Begin
Backup database name to disk = @ devname_data

With init
End
If @ baklog = 0
Begin
Backup log database name with no_log

Backup log database name to disk = @ devname_log

With init, no_truncate
End
End
Else begin
If @ bakdb = 0
Begin
Backup database name to disk = @ devname_data

With noinit
End
If @ baklog = 0
Begin
Backup log database name with no_log

Backup log database name to disk = @ devname_log

With noinit, no_truncate
End
End
End
Select @ retmsg = 'database backup successful! '
End

If @ kind = 'restore'
Begin
Restore database name from disk = @ devname_data with replace

Select @ retmsg = 'the database is restored successfully! '
End

Exec sp_dropdevice @ devname_data
Exec sp_dropdevice @ devname_log

Return 0

--- Test:
Declare @ varchar (100)
Exec gy_dbbak 0, 'A', 0, 0, 0, 'backup ', @ output
Select @
-------------------------------------------------
Backup device:
Sp_helpdevice
Or

Use master
Select * From sysdevices

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.