亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

c#中數據庫的備份和恢復

系統 1986 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();
?? }

?

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();
?? }
}
}

?

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;
?? }
}
}

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;
?? }
}
}

?

c#中數據庫的備份和恢復


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品操 | 五月天天爱 | 伊人久久综合视频 | 亚洲欧美国产精品专区久久 | 日本伊人精品一区二区三区 | 一区二区三区高清在线 | 成人欧美一区二区三区 | 久久婷婷综合在线视频观看6 | 一级aa毛片| 狠狠的操你| 日韩专区第一页 | 精品精品国产理论在线观看 | 欧美日本中文字幕 | 久草久在线| 四虎影视在线观看永久地址 | 99在线视频网站 | 日本老乱video | 久久综合九色综合精品 | 狠狠色噜噜狠狠狠合久 | 天天操视频 夜夜 | 亚洲欧美在线精品一区二区 | 日本一级片免费观看 | 午夜男人影院 | 国产成人精品久久二区二区 | 国产欧美一区二区三区观看 | 中文字幕1区2区 | 国产精品自在自线免费观看 | 一道本一区二区三区 | 91久久精品国产91性色tv | 欧美肥婆videos另类 | 久久综合九色综合97_ 久久久 | 色域综合 | 国产亚洲精品美女久久久久 | 91视频成人| 久久国产精品99精品国产987 | 夜夜橹| 亚洲欧美中文字幕高清在线一 | 久久综合给会久久狠狠狠 | 女性一级全黄生活片在线播放 | 国产乱码精品一区二区三区四川 | 日本不卡在线视频 |