. Method of backing up SQL Server database in net

Source: Internet
Author: User
Tags datetime sql net string tostring backup

When we are developing a Web site, in the admin background, the administrator usually makes a regular backup of the database (and, of course, the SQL Server can be automatically backed up regularly, but I'm talking about backup in asp.net), and the backup code is simple:

Here is an event I write under the backup button when I do a website backstage:

protected void Button1_Click (object sender, EventArgs e)

... {

String newname = "Webjake" + DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + DateTime.Now.Day.ToString () + DateTime.Now.Hour.ToString () + ". Bak";

SqlConnection cn = Db.createcon ();

String Nepath=server.mappath (".. /.. /databack/") +newname;

String sql = "BACKUP DATABASE webjake to DISK = '" +nepath+ "";

SqlCommand cmd = new SqlCommand (SQL,CN);

Try

... {

cn. Open ();

Cmd. ExecuteNonQuery ();

Hyperlink1.text = "Successfully backed up to server path: + nepath+" click Download to local! ";

Hyperlink1.navigateurl = ".. /.. /databack/"+newname;

Hyperlink1.visible = true;

}

catch (Exception ex)

... {

String EXM = ex. message;

Label1.Text = "BACKUP Database error, this file may not exist!" ";

Label1.visible = true;

}

Finally

... {

Cmd. Dispose ();

cn. Close ();

cn. Dispose ();

}

Of course, the most important thing is this:

BACKUP DATABASE webjake to DISK = ' The path you want to save '



Related Article

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.