日期:2014-05-18  浏览次数:20685 次

请教数据库备份还原例子
RT   实现就结帖

------解决方案--------------------
protected void btnBackup_Click(object sender, EventArgs e)
{
string cmdtxt1 = "Server=(local);Database=master;Uid=sa;Pwd= ' "+this.txtPwd.Text.Trim()+ " ' ";
string cmdtxt2 = "USE master EXEC xp_cmdshell 'bcp "+this.dropDatabase.SelectedValue+ ".dbo. "+this.dropTable.SelectedValue+ " ";
cmdtxt2 += " out "+this.txtPath.Text.Trim()+ ".xls -c -q -S. -Usa -P "+this.txtPwd.Text.Trim()+ " ' ";
SqlConnection Con;
try
{
Con = new SqlConnection(cmdtxt1);
Con.Open();
SqlCommand Com = new SqlCommand(cmdtxt2, Con);
Com.ExecuteNonQuery();
Con.Close();
Response.Write( " <script language=javascript> alert( '备份数据成功! ');location= 'Index1.aspx ' </script> ");
}
catch (Exception ms)
{
Response.Write(ms.Message);
Response.Write( " <script language=javascript> alert( '备份数据失败! ');location= 'Index1.aspx ' </script> ");
}
}
protected void btnRestore_Click(object sender, EventArgs e)
{
string cmdtxt1 = "Server=(local);Database=master;Uid=sa;Pwd= ' " + this.txtPwd.Text.Trim() + " ' ";
string cmdtxt2 = "USE master EXEC xp_cmdshell 'bcp " + this.dropDatabase.SelectedValue + ".dbo. " + this.dropTable.SelectedValue + " ";
cmdtxt2 += " in " + this.fileShow.PostedFile.FileName + " -c -q -S. -Usa -P " + this.txtPwd.Text.Trim() + " ' ";
SqlConnection Con;
try
{
Con = new SqlConnection(cmdtxt1);
Con.Open();
SqlCommand Com = new SqlCommand(cmdtxt2, Con);
Com.ExecuteNonQuery();
Con.Close();
Response.Write( " <script language=javascript> alert( '还原数据成功! ');location= 'Index1.aspx ' </script> ");
}
catch (Exception ms)
{
Response.Write(ms.Message);
Response.Write( " <script language=javascript> alert( '还原数据失败! ');location= 'Index1.aspx ' </script> ");
}
}

------解决方案--------------------
你是要手工?