日期:2014-05-18  浏览次数:20451 次

C#代码备份与还原数据库
C# code

string myStr = System.Configuration.ConfigurationManager.AppSettings["98lunwen"];
protected void btn_backup_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection(myStr);
        string SqlStr1 = "Exec sp_helpdb";
        myConn.Open();
        SqlCommand com = new SqlCommand(SqlStr1, myConn);
        SqlDataReader dr = com.ExecuteReader();
        string SqlStr2 = "backup database lunwena to disk='~/Admin/BAK/lunwena_B.bak'";
        
        try
        {
            SqlCommand com1 = new SqlCommand(SqlStr2, myConn);
            com1.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('备份数据成功!')</script>");
        }
        catch (Exception error)
        {
            Response.Write(error.Message);
            Response.Write("<script language=javascript>alert('备份数据失败!')</script>");
        }
        finally
        {
            myConn.Close();
        }       
        Response.Redirect("Web_Database.aspx");
    }
    protected void btn_return_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection(myStr);
        string SqlStr1 = "Exec sp_helpdb";
        myConn.Open();
        SqlCommand com = new SqlCommand(SqlStr1, myConn);
        SqlDataReader dr = com.ExecuteReader();
        string SqlStr2 = "use master restore database lunwena_B from disk='~/Admin/BAK/lunwena_B.bak'";
        
        try
        {
            SqlCommand com1 = new SqlCommand(SqlStr2, myConn);
            com1.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('还原数据成功!')</script>");
        }
        catch (Exception error)
        {
            Response.Write(error.Message);
            Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
        }
        finally
        {
            myConn.Close();
        }
        Response.Redirect("Web_Database.aspx");
    }



以上代码能不能备份与还原数据库呢,各位高手指点一下,如果不能要怎样改??

------解决方案--------------------
不一定,备份SqlServer 数据库,不一定仅仅是sql指令是不是正确,还有连接,备份可以不断开连接,但还原需要断开连接。备份还要考虑备份位置有没有读写权限。
------解决方案--------------------
调用存储过程或执行语句
CREATE proc dbo.Data_Backup @dbname sysname='',@bkpath nvarchar(260)='',@bkfname nvarchar(260)='',@bktype nvarchar(10)='DB',@appendfile bit=1
as declare @sql varchar(8000) if isnull(@dbname,'')='' set @dbname=db_name() if isnull(@bkfname,'')=''
set @bkfname='\DBNAME\_\DATE\_\TIME\.BAK' set @bkfname=replace(replace(replace(@bkfname,'\DBNAME\',@dbname),'\DATE\',convert(varchar,getdate(),112)),'\TIME\',replace(convert(varchar,getdate(),108),':',''))
update XTSZ set FNAME=@bkfname
set @sql='backup '+case @bktype when 'LOG' then 'log ' else 'database ' end +@dbname +' to disk='''+@bkpath+@bkfname +''' with '+case @bktype when 'DF' then 'DIFFERENTIAL,' else '' end+case @appendfile when 1 then 'NOINIT' else 'INIT' end
exec(@sql)

USE master
EXEC sp_addumpdevice 'disk', 'MyNwind',
'c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyNwind.dat'

------解决方案--------------------
我这里有个备份oracle 和图片的 批处理,你可以看下
@echo off
@echo =======================================================================
@echo 配置
set fileup=\\Web-1\FileUpload\fileUp\
set imges=\\Web-1\FileUpload\WebImages\ManageImages\NewsImages\
set saveurl=c:\11\%date:~0,10%\
@echo 创建备份目录(%saveurl%).....
md %saveurl%
@echo =======================================================================
@echo 备份数据库
@echo ===========================================