数据还原
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();
}