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 ();
}