Back up and restore an SQL Server database in ASP. NET

Source: Internet
Author: User

I would like to introduce how to use ASP. NET backup to restore the SQL Server database. You can make a reference and hope to help you. Back up the sqlserver Database:
The following is a reference segment:
string sqlstr1 = "Server = (local); database = '" + this. dropdownlist1.selectedvalue + "'; uid = sa; Pwd =";
string sqlstr2 = "backup database" + this. dropdownlist1.selectedvalue + "to disk = '" + this. textbox1.text. trim () + ". bak '";
sqlconnection con = new sqlconnection (sqlstr1);
con. open ();
try
{< br> If (file. exists (this. textbox1.text. trim ()
{< br> response. write ("");
return;
}< br> sqlcommand COM = new sqlcommand (sqlstr2, con);
COM. executenonquery ();
response. write ("");
}< br> catch (Exception error)
{< br> response. write (error. message);
response. write ("");
}< br> finally
{< br> con. close ();
}

restore the sqlserver Database:
The following is a reference segment:
string Path = This. fileupload1.postedfile. filename; // obtain the backup path and database name
string dbname = This. dropdownlist1.selectedvalue;
string sqlstr1 = "Server = (local); database = '" + this. dropdownlist1.selectedvalue + "'; uid = sa; Pwd = ";
string sqlstr2 = "use master Restore database" + dbname + "from disk = '" + path + "'";
sqlconnection con = new sqlconnection (sqlstr1 );
con. open ();
try
{< br> sqlcommand COM = new sqlcommand (sqlstr2, con);
COM. executenonquery ();
response. write ("");
}< br> catch (Exception error)
{< br> response. write (error. message);
response. write ("");
}< br> finally
{< br> con. close ();
}

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.