David Liao

为家人活得更好而奋斗!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

C#数据库备份恢复这样简单

这是我附的源代码,实现的备份和恢复很简单,大家可以自己在补充....(有BUG的话,欢迎指教)

这是WEB后台程序

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string path=@"c:\Test.bak";
        
string backupstr="backup database Test to disk='"+path+"';";
        SqlConnection con 
= new SqlConnection("server=.;uid=sa;pwd=;");
        SqlCommand cmd 
= new SqlCommand(backupstr, con);
        
try
        
{
            con.Open();
            cmd.ExecuteNonQuery();
            
this.Label1.Text = "备份成功!";

        }

        
catch
        
{
            
this.Label1.Text = "备份失败!";
        }

        
finally
        
{
            con.Close();
        }

    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{
        
string path = @"c:\Test.bak";
        
string restore="restore database Test from disk='"+path+"';";
        SqlConnection con
=new SqlConnection("server=.;uid=sa;pwd=;");
        SqlCommand cmd
=new SqlCommand(restore,con);
        
try
        
{
            con.Open();
            cmd.ExecuteNonQuery();
            
this.Label1.Text = "恢复成功";

        }

        
catch
        
{
            
this.Label1.Text = "恢复失败";
        }

        
finally
        
{
            con.Close();
        }

    }

}


posted on 2007-03-07 11:18  David Liao  阅读(1545)  评论(0)    收藏  举报