sql server备份与还原 sql语句
数据还原
SqlConnection conn = new SqlConnection(ConnectionString);
if (this.txtPlace.Text != "")
{
try
{
string P_Str_cmdtxt = "USE master DECLARE tb
CURSOR LOCAL FOR SELECT ‘Kill ‘+ CAST(Spid AS VARCHAR) FROM
master.dbo.sysprocesses";
P_Str_cmdtxt += " WHERE
dbid=DB_ID(‘db_GoodsManage‘) DECLARE @s nvarchar(1000) OPEN tb FETCH tb INTO
@s";
P_Str_cmdtxt += " WHILE @@FETCH_STATUS = 0 BEGIN
EXEC (@s) FETCH tb INTO @s END CLOSE tb DEALLOCATE tb";
P_Str_cmdtxt += " RESTORE DATABASE db_GoodsManage FROM disk=‘" +
txtPlace.Text.Trim() + "‘";
conn.Open();
SqlCommand cmd = new
SqlCommand(P_Str_cmdtxt, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("数据还原成功");
}
catch
{
MessageBox.Show("数据还原失败");
return;
}
finally
{
conn.Close();
}
数据同步
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
if ((txtPlace.Text.Trim() != "")
&& (txtBakName.Text.Trim() != ""))
{
string cmdtxt = "backup database db_GoodsManage to disk=‘" +
txtPlace.Text.Trim() + "\\" + txtBakName.Text.Trim()+ ".bak‘";
conn.Open();
SqlCommand cmd = new
SqlCommand(cmdtxt,conn);
cmd.ExecuteNonQuery();
MessageBox.Show("备份成功");
return;
}
else
{
MessageBox.Show("备份失败");
return;
}
}
catch(Exception ee)
{
MessageBox.Show("备份失败"+ee.ToString());
return;
}
finally
{
conn.Close();
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。