在c#中SQL数据备份和还原

 

/// <summary> 
/// 备份数据库 
/// </summary> 
/// <param name="mdb1">源数据库绝对路径</param> 
/// <param name="mdb2">目标数据库绝对路径</param> 
/// <returns></returns> 
public void backup_db(string mdb1, string mdb2) 

if (mdb1 == ""

mdb1 
= HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 

if (!File.Exists(mdb1)) 

throw new Exception("源数据库不存在"); 

try 

File.Copy(mdb1, mdb2, 
true); 

catch (IOException ixp) 

throw new Exception(ixp.ToString()); 


/// <summary> 
/// 恢复数据库 
/// </summary> 
/// <param name="mdb1">备份数据库绝对路径</param> 
/// <param name="mdb2">当前数据库绝对路径</param> 
/// <returns></returns> 
public void restore_db(string mdb1, string mdb2) 

if (mdb2 == ""

mdb2 
= HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 

if (!File.Exists(mdb1)) 

throw new Exception("备份数据库不存在"); 

try 

File.Copy(mdb1, mdb2, 
true); 

catch (IOException ixp) 

throw new Exception(ixp.ToString()); 




备份 
Private 
void back() 

System.Random myRd 
= new Random(); 
string SQLFile = Request.MapPath("blogDB/"+ DateTime.Now.ToString().Replace("-""").Replace(":""").Replace(" """+ myRd.Next(1000+ ".DAK"
try 

SQL_db.backup_db(
"", SQLFile); 
Jscript.Alert(
"成功!"); 

catch 

Jscript.Alert(
"失败!"); 

BindSQLFile(); 

还原 
private void restore() 

string SQLFile = Request.PhysicalApplicationPath + @"blogDB\" +SQLFiles.DataKeys[Convert.ToInt32(e.CommandArgument)]["FileName"].ToString(); 

try 

SQL_db.restore_db(SQLFile, 
""); 
Jscript.Alert(
"成功!"); 

catch 

Jscript.Alert(
"失败!"); 


posted @ 2008-08-19 17:20  supers  阅读(377)  评论(0)    收藏  举报