Core technologies:
Using system. Data. sqlclient;
Using system. IO;
String sqlstr1 = "Server = (local); database = Master; uid = sa; Pwd = ";
String sqlstr2 = "Exec sp_helpdb ";
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 '";
1. Front-end
<Table>
<Tr>
<TD style = "width: 100px"> <span style = "font-size: 9pt"> operational data warehouse </span> </TD>
<TD> <asp: dropdownlist id = "dropdownlist1" runat = "server" font-size = "9pt" width = "124px"> </ASP: dropdownlist> </TD>
<TD style = "width: 100px"> </TD>
</Tr>
<Tr>
<TD style = "width: 100px"> <span style = "font-size: 9pt"> Backup name and location </span> </TD>
<TD style = "width: 100px"> <asp: textbox id = "textbox1" runat = "server" font-size = "9pt" width = "117px"> </ASP: textbox> </TD>
<TD style = "width: 100px"> <span style = "font-size: 9pt; color: # ff3300"> (for example, D: \ beifen) </span> </TD>
</Tr>
<Tr>
<TD colspan = "3"> <asp: button id = "button1" runat = "server" font-size = "9pt" onclick = "button#click" text = "backup database"/> </TD>
</Tr>
</Table>
2. Background
Using system. Data. sqlclient;
Using system. IO;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
String sqlstr1 = "Server = (local); database = Master; uid = sa; Pwd = ";
String sqlstr2 = "Exec sp_helpdb ";
Sqlconnection con = new sqlconnection (sqlstr1 );
Con. open ();
Sqlcommand COM = new sqlcommand (sqlstr2, con );
Sqldatareader DR = com. executereader ();
This. dropdownlist1.datasource = Dr;
This. dropdownlist1.datatextfield = "name ";
This. dropdownlist1.databind ();
Dr. Close ();
Con. Close ();
}
}
Protected void button#click (Object sender, eventargs E)
{
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
{
If (file. exists (this. textbox1.text. Trim ()))
{
Response. Write ("<script language = JavaScript> alert ('this file already exists. Please input it again! '); Location = 'default. aspx' </SCRIPT> ");
Return;
}
Sqlcommand COM = new sqlcommand (sqlstr2, con );
Com. executenonquery ();
Response. Write ("<script language = JavaScript> alert ('data backed up successful! '); Location = 'default. aspx' </SCRIPT> ");
}
Catch (Exception error)
{
Response. Write (error. Message );
Response. Write ("<script language = JavaScript> alert ('data backup failed! ') </SCRIPT> ");
}
Finally
{
Con. Close ();
}
}
}
Restore sqlserver
Core technologies:
String sqlstr1 = "Server = (local); database = '" + this. dropdownlist1.selectedvalue + "'; uid = sa; Pwd = ";
String sqlstr2 = "use master Restore database" + dbname + "from disk = '" + path + "'";
1. Front-end
<Table>
<Tr>
<TD style = "width: 100px; Height: 21px"> <span style = "font-size: 9pt"> operation data warehouse </span> </TD>
<TD> <asp: dropdownlist id = "dropdownlist1" runat = "server" font-size = "9pt" width = "124px"> </ASP: dropdownlist> </TD>
<TD style = "width: 100px; Height: 21px"> </TD>
</Tr>
<Tr>
<TD style = "width: 100px"> <span style = "font-size: 9pt"> operational data warehouse </span> </TD>
<TD style = "width: 100px"> <asp: fileupload id = "fileupload1" runat = "server" font-size = "9pt" width = "pixel PX"/> </TD>
<TD style = "width: 100px">
</TD>
</Tr>
<Tr>
<TD colspan = "3"> <asp: button id = "button1" runat = "server" font-size = "9pt" onclick = "button#click" text = "Restore database"/> </TD>
</Tr>
</Table>
2. Background
Using system. Data. sqlclient;
Using system. IO;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
String sqlstr1 = "Server = (local); database = Master; uid = sa; Pwd = ";
String sqlstr2 = "Exec sp_helpdb ";
Sqlconnection con = new sqlconnection (sqlstr1 );
Con. open ();
Sqlcommand COM = new sqlcommand (sqlstr2, con );
Sqldatareader DR = com. executereader ();
This. dropdownlist1.datasource = Dr;
This. dropdownlist1.datatextfield = "name ";
This. dropdownlist1.databind ();
Dr. Close ();
Con. Close ();
}
}
Protected void button#click (Object sender, eventargs E)
{
String Path = This. fileupload1.postedfile. filename; // obtain the backup path and database name.
String dbname = This. dropdownlist2.selecteditem. value;
String sqlstr1 = "Server =.; database = Master; uid = sa; Pwd = ";
String sqlstr2 = "use master Restore database" + dbname + "from disk = '" + path + "'";
String sqlstr3 = "Exec killspid 'tw _ zlgd '";
Sqlconnection con = new sqlconnection (sqlstr1 );
Con. open ();
Try
{
Sqlcommand com2 = new sqlcommand (sqlstr3, con );
Com2.executenonquery ();
Sqlcommand COM = new sqlcommand (sqlstr2, con );
Com. executenonquery ();
MessageBox. Show (this, "restored data! ");
}
Catch (Exception error)
{
MessageBox. Show (this, "failed to restore Data! Cause: "+ error. Message );
}
Finally
{
Con. Close ();
}
}
You need to create a stored procedure named "killspid" under the master database.
Create proc killspid (@ dbname varchar (20 ))
As
Begin
Declare @ SQL nvarchar (500)
Declare @ spid int
Set @ SQL = 'Clare getspid cursor for select spid from sysprocesses where dbid = db_id (''' + @ dbname + ''')'
Exec (@ SQL)
Open getspid
Fetch next from getspid into @ spid
While @ fetch_status <>-1
Begin
Exec ('Kill '+ @ spid)
Fetch next from getspid into @ spid
End
Close getspid
Deallocate getspid
End
Go
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yytt123622/archive/2009/01/14/3765641.aspx