C#如何实现还原数据库操作 已有一个textbox和openFileDialog获取文件,用一个“还原”按钮实现
2个回答
展开全部
很久之前写的2个 方法
/// <summary>
/// 备份数据库
/// </summary>
public int BackData(string path)
{
string sql = "DECLARE @strPath NVARCHAR(200) ";
sql += "set @strPath = convert(NVARCHAR(19),getdate(),112) ";
sql += " set @strPath = REPLACE(@strPath, ':' , '-') ";
sql += "set @strPath=replace(@strPath,' ','_') ";
sql += "set @strPath = '" + path + "'+'\\'+'cnwinetour' +@strPath + '.bak' ";
sql += "BACKUP DATABASE cnwinetour TO DISK = @strPath WITH NOINIT , NOUNLOAD , NOSKIP , STATS = 10, NOFORMAT ";
int n = DBHelper.ExecuteNonQuery(sql, CommandType.Text);
return n;
}
/// <summary>
/// 还原数据库
/// </summary>
public int RecoveryData(string path)
{
string sql = " USE tempdb RESTORE DATABASE cnwinetour FROM DISK=" + path + " WITH REPLACE,RECOVERY ";//NORECOVERY
int n = DBHelper.ExecuteNonQuery(sql,CommandType.Text);
return n;
}
/// <summary>
/// 备份数据库
/// </summary>
public int BackData(string path)
{
string sql = "DECLARE @strPath NVARCHAR(200) ";
sql += "set @strPath = convert(NVARCHAR(19),getdate(),112) ";
sql += " set @strPath = REPLACE(@strPath, ':' , '-') ";
sql += "set @strPath=replace(@strPath,' ','_') ";
sql += "set @strPath = '" + path + "'+'\\'+'cnwinetour' +@strPath + '.bak' ";
sql += "BACKUP DATABASE cnwinetour TO DISK = @strPath WITH NOINIT , NOUNLOAD , NOSKIP , STATS = 10, NOFORMAT ";
int n = DBHelper.ExecuteNonQuery(sql, CommandType.Text);
return n;
}
/// <summary>
/// 还原数据库
/// </summary>
public int RecoveryData(string path)
{
string sql = " USE tempdb RESTORE DATABASE cnwinetour FROM DISK=" + path + " WITH REPLACE,RECOVERY ";//NORECOVERY
int n = DBHelper.ExecuteNonQuery(sql,CommandType.Text);
return n;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询