| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1024 人关注过本帖
标题:[求助]数据库备份
只看楼主 加入收藏
低级编程者
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2006-4-9
收藏
 问题点数:0 回复次数:13 
[求助]数据库备份

在C#中,如何实现数据库备份和数据恢复呢?

搜索更多相关主题的帖子: 数据库 数据恢复 
2006-06-30 13:39
xupeng
Rank: 1
等 级:新手上路
帖 子:4049
专家分:0
注 册:2006-2-12
收藏
得分:0 
也是执行SQL语句:
backup database DB_name to disk='D:\XXX' //备份
restore database DB_name from disk='D:\XXX' //恢复备份
注意:XXX是不加后缀名的

反清复明 http://xupeng.
2006-06-30 14:16
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

//数据备份菜单点击事件
private void mnuBackUp_Click(object sender, System.EventArgs e)
{
frmBackUp back=new frmBackUp(frmBackUp.SetType.BackUp);
back.Show();
//调用dll
BackUpAndReinstate.BackUp backup=new BackUpAndReinstate.BackUp(Application.StartupPath+"\\BackUp\\TeachingBusiness.bak");
string message=backup.DataBaseBackUp();
MessageBox.Show(message,"消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
back.Close();
}

//数据恢复菜单点击事件
private void mnuReinstate_Click(object sender, System.EventArgs e)
{
frmBackUp back=new frmBackUp(frmBackUp.SetType.Reinstate);
back.Show();
//调用dll
BackUpAndReinstate.Reinstate reinstate=new BackUpAndReinstate.Reinstate();
string message=reinstate.DataBaseReinstate();
MessageBox.Show(message,"消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
back.Close();
}


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-06-30 15:05
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

frmBackUp窗体代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace 教务系统
{
/// <summary>
/// frmBackUp 的摘要说明。
/// </summary>
public class frmBackUp : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;

public enum SetType
{
BackUp,
Reinstate
}
private int type=0;
public frmBackUp(SetType settype)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.type=(int)settype;
//
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmBackUp));
this.label1 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(256, 64);
this.label1.TabIndex = 0;
this.label1.Text = "正在备份数据,请稍侯......";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(24, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(64, 48);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// timer1
//
this.timer1.Interval = 300;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// frmBackUp
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 80);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label1);
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "frmBackUp";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "备份数据...";
this.Load += new System.EventHandler(this.frmBackUp_Load);
this.ResumeLayout(false);

}
#endregion

private void timer1_Tick(object sender, System.EventArgs e)
{
this.pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);
this.pictureBox1.Refresh();
}

// public void setType(SetType)
// {
//
private void frmBackUp_Load(object sender, System.EventArgs e)
{
if(this.type==(int)SetType.BackUp)
this.label1.Text="正在备份数据,请稍侯......";
else if(this.type==(int)SetType.Reinstate)
this.label1.Text="正在恢复数据,请稍侯......";
this.timer1.Start();
}
}
}


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-06-30 15:06
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

BackUpAndReinstate组件中的类:(BackUp类和Reinstate类)

BackUp类:

using System;
using System.Data;
using System.Data.SqlClient;

namespace BackUpAndReinstate
{
/// <summary>
/// 数据备份类
/// </summary>
public class BackUp
{
private SqlConnection con=null; //数据库连接对象
private string filepath=null; //备份路径

//单参构造
public BackUp(string filepath)
{
con=new SqlConnection("database=master;user id=sa;password=sa;data source=(local)");
this.filepath=filepath;
}

//数据备份方法
public string DataBaseBackUp()
{
string str="";
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="select name from sysdevices where name='TeachingBusiness'";
con.Open();
SqlDataReader rdr=cmd.ExecuteReader();
if(!rdr.Read())
{
rdr.Close();
SqlCommand backcmd=new SqlCommand();
backcmd.Connection=con;
backcmd.CommandText="EXEC sp_addumpdevice @devtype,@logicalname,@physicalname";
SqlParameter param=backcmd.Parameters.Add("@devtype",SqlDbType.VarChar,20);
param.Value="disk";
param=backcmd.Parameters.Add("@logicalname",SqlDbType.VarChar,20);
param.Value="TeachingBusiness";
param=backcmd.Parameters.Add("@physicalname",SqlDbType.NVarChar,260);
param.Value=this.filepath;
backcmd.ExecuteNonQuery();
}
rdr.Close();
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException er)
{
str=er.Message;
return str;
}
cmd.CommandText="backup database TeachingBusiness to TeachingBusiness";
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException er)
{
str=er.Message;
return str;
}
finally
{
con.Close();
}
str="已备份成功!";
return str;
}
}
}

Reinstate类:

using System;
using System.Data;
using System.Data.SqlClient;

namespace BackUpAndReinstate
{
/// <summary>
/// 数据恢复类
/// </summary>
public class Reinstate
{
private SqlConnection con=null; //数据库连接对象

//默认构造
public Reinstate()
{
con=new SqlConnection("database=master;user id=sa;password=sa;data source=(local)");
}

//数据恢复方法
public string DataBaseReinstate()
{
string str="";
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="RESTORE DATABASE TeachingBusiness FROM TeachingBusiness with replace";
con.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException er)
{
str=er.Message;
return str;
}
finally
{
con.Close();
}
str="已成功恢复数据库";
return str;
}
}
}

[此贴子已经被作者于2006-6-30 15:12:28编辑过]


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-06-30 15:10
xupeng
Rank: 1
等 级:新手上路
帖 子:4049
专家分:0
注 册:2006-2-12
收藏
得分:0 
以下是引用xupeng在2006-6-30 14:16:17的发言:
也是执行SQL语句:
backup database DB_name to disk='D:\XXX' //备份
restore database DB_name from disk='D:\XXX' //恢复备份
注意:XXX是不加后缀名的

用我的比楼上的简单!


反清复明 http://xupeng.
2006-06-30 15:12
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
是啊,你的简单,我用得也就是上面那两句,只是扩展的比较多。学习编程不能以简单为主!!

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-06-30 15:13
xupeng
Rank: 1
等 级:新手上路
帖 子:4049
专家分:0
注 册:2006-2-12
收藏
得分:0 

反清复明 http://xupeng.
2006-06-30 15:49
低级编程者
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2006-4-9
收藏
得分:0 
非常感谢各位~~`

知道爱的美妙,才会如此渴望去拥有!
2006-06-30 16:21
低级编程者
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2006-4-9
收藏
得分:0 

你的是简单,但是我不会用呀?
能不能举一下例子啊?
谢谢


知道爱的美妙,才会如此渴望去拥有!
2006-06-30 16:32
快速回复:[求助]数据库备份
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035124 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved