backup.cs
上传用户:toshiba_01
上传日期:2007-06-08
资源大小:306k
文件大小:5k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data ;
using System.Data .SqlClient ;
- namespace RoomManage
- {
- /// <summary>
- /// backup 的摘要说明。
- /// </summary>
- public class backup : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.Button button3;
- private System.Windows.Forms.TextBox tb;
- private System.Windows.Forms.FolderBrowserDialog fbd;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public backup()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <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.button1 = new System.Windows.Forms.Button();
- this.label1 = new System.Windows.Forms.Label();
- this.tb = new System.Windows.Forms.TextBox();
- this.button2 = new System.Windows.Forms.Button();
- this.button3 = new System.Windows.Forms.Button();
- this.fbd = new System.Windows.Forms.FolderBrowserDialog();
- this.SuspendLayout();
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(232, 16);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(56, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "浏览";
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(8, 16);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(56, 23);
- this.label1.TabIndex = 1;
- this.label1.Text = "备份到:";
- this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
- //
- // tb
- //
- this.tb.Location = new System.Drawing.Point(56, 16);
- this.tb.Name = "tb";
- this.tb.Size = new System.Drawing.Size(168, 21);
- this.tb.TabIndex = 2;
- this.tb.Text = "";
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(40, 80);
- this.button2.Name = "button2";
- this.button2.TabIndex = 0;
- this.button2.Text = "确定";
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // button3
- //
- this.button3.Location = new System.Drawing.Point(160, 80);
- this.button3.Name = "button3";
- this.button3.TabIndex = 1;
- this.button3.Text = "退出";
- this.button3.Click += new System.EventHandler(this.button3_Click);
- //
- // fbd
- //
- this.fbd.Description = "在此选择要将备份文件保存到的目录";
- //
- // backup
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(292, 125);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.tb);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.button3);
- this.MaximizeBox = false;
- this.MaximumSize = new System.Drawing.Size(300, 152);
- this.MinimizeBox = false;
- this.MinimumSize = new System.Drawing.Size(300, 152);
- this.Name = "backup";
- this.ShowInTaskbar = false;
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
- this.Text = "数据备份";
- this.ResumeLayout(false);
- }
- #endregion
- private void button1_Click(object sender, System.EventArgs e)
- {
- if (fbd.ShowDialog() == DialogResult.OK)
- {
- this.tb .Text =this.fbd.SelectedPath ;
- }
- else
- {
- this.tb .Text ="";
- }
- }
- private void button2_Click(object sender, System.EventArgs e)
- {
- DataConn dc=new DataConn ();
- string str=dc.connstr ;
- SqlConnection conn=new SqlConnection (str);
-
- string dirstr=this.tb .Text+"\BK"+(System.DateTime.Today .ToShortDateString())+".dat";//以BK+当前日期的形式作为备份文件的名字
- string sql="backup DATABASE RoomManage TO disk='"+dirstr+"'";
- SqlCommand comd=new SqlCommand (sql,conn);
- conn.Open ();
- try
- {
- comd.ExecuteNonQuery ();
- MessageBox.Show ("备份成功!","",MessageBoxButtons.OK ,MessageBoxIcon.Information );
- tb.Text ="";
-
- }
- catch
- {
- MessageBox.Show ("备份失败!请仔细检查,重新备份。","",MessageBoxButtons.OK,MessageBoxIcon.Error );
- }
- finally
- {
- conn.Close ();
- comd.Dispose ();
- }
- }
- private void button3_Click(object sender, System.EventArgs e)
- {
- if(MessageBox.Show("真的要退出吗?","注意",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
- this.Close ();
- }
- }
- }