程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#數據庫的備份(SQL2005)

C#數據庫的備份(SQL2005)

編輯:C#入門知識

[csharp]
using System; 
[csharp] 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.IO; 
namespace WindowsApplication1 

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            this.txtName.Text = "數據庫名" + System.DateTime.Now.ToShortDateString(); 
        } 
        //將數據庫備份到應用程序的跟目錄下 
        string connectionString = "server=服務器名;database=數據庫名;uid=用戶名;pwd=密碼"; 
        
        private void btn_beifen_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                string strg = Application.StartupPath.ToString(); 
                strg = strg.Substring(0, strg.LastIndexOf("\\")); 
                strg = strg.Substring(0, strg.LastIndexOf("\\")); 
                strg += @"\Data"; 
                string sqltxt = @"BACKUP DATABASE 數據庫名 TO Disk='" + strg + "\\" + txtPath.Text + ".bak" + "'"; 
                SqlConnection con = new SqlConnection(connectionString); 
                con.Open(); 
                SqlCommand cmd = new SqlCommand(sqltxt, con); 
                cmd.ExecuteNonQuery(); 
                con.Close(); 
                if (MessageBox.Show("備份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) 
                { 
                    this.Close(); 
                } 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message.ToString()); 
            } 
        } 
        //選擇保存目錄事件 
        private void btn_open_Click(object sender, EventArgs e) 
        { 
            folderBrowserDialog1.Description = "請選擇備份文件將要保存到的文件夾,如有必要你可以\n通過單擊左下角的‘新建文件夾’按鈕新建文件夾"; 
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
            { 
                txtPath.Text = folderBrowserDialog1.SelectedPath.ToString(); 
            } 
        } 
        //開始備份數據到指定目錄下的指定文件名 
        private void btn_ok_Click(object sender, EventArgs e) 
        { 
            string filepath="D:\\機房設備資源管理系統數據庫備份\\"; 
            try 
            { 
                if (txtName.Text != "") 
                { 
                    if (txtPath.Text != "") 
                    { 
                         filepath=txtPath.Text.Trim()+"\\"; 
                    }else 
                    { 
                        if (MessageBox.Show("你確定要要將數據備份到默認位置:" + filepath + "下嗎?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) 
                        { 
                            MessageBox.Show("請選擇保存位置!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                            btn_open_Click(sender, e); 
                        } 
                        else  
                        { 
                            if (!Directory.Exists(filepath)) 
                            { 
                                Directory.CreateDirectory(filepath); 
                            } 
                        } 
                    } 
                    SqlConnection con = new SqlConnection(connectionString); 
                    con.Open(); 
                    string strBacl = "backup database 數據庫名 to disk='" + filepath + txtName.Text.Trim() + ".bak'"; 
                    SqlCommand Cmd = new SqlCommand(strBacl, con); 
                    if (Cmd.ExecuteNonQuery() != 0) 
                    { 
                        con.Close(); 
                        MessageBox.Show("數據備份成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                        this.Close(); 
                    } 
                    else 
                    { 
                        MessageBox.Show("數據備份失敗!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    } 
                } 
                else 
                if (txtPath.Text != "" && txtName.Text== "") 
                { 
                    MessageBox.Show("備份名稱為必填項!你必須填寫!:", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                }// end 
            } 
            catch (Exception ee) 
            { 
                MessageBox.Show(ee.Message.ToString()); 
            } 
        } 
        private void LK_lbl_option_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
        { 
            restore rs = new restore(); 
            rs.Show(); 
        } 
  
 
    } 


SQL數據庫恢復後台代碼:
[csharp]
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
namespace WindowsApplication1 

    public partial class restore : Form 
    { 
        public restore() 
        { 
            InitializeComponent(); 
        } 
        string DateStr = "server=服務器名;database=數據庫名;uid=用戶名;pwd=密碼"; 
        private void restore_Load(object sender, EventArgs e) 
        { 
            //string strg = Application.StartupPath.ToString(); 
            //strg = strg.Substring(0, strg.LastIndexOf("\\")); 
            //strg = strg.Substring(0, strg.LastIndexOf("\\")); 
            //strg += @"\Data"; 
            //textBox1.Text = strg + "\\" + "MySale.bak"; 
        } 
        /// <summary> 
        /// 備份數據庫按鈕事件 
        /// txtName(文本框)用於獲取備份文件名,txtPath(文本框)用於獲取備份文件路徑 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        
        private void btn_ok_Click(object sender, EventArgs e) 
        { 
            if (this.txtPath.Text != ""&&this.txtPath.Text != null) 
            { 
                SqlConnection conn = new SqlConnection(DateStr); 
                conn.Open(); 
                //-------------------殺掉所有連接 db_PWMS 數據庫的進程-------------- 
                string strSQL = "select spid from master..sysprocesses where dbid=db_id( '數據庫名') "; 
                SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn); 
                DataTable spidTable = new DataTable(); 
                Da.Fill(spidTable); 
                SqlCommand Cmd = new SqlCommand(); 
                Cmd.CommandType = CommandType.Text; 
                Cmd.Connection = conn; 
                if (spidTable.Rows.Count > 1) 
                {//強行關閉非本程序使用的所有用戶進程 
                    for (int iRow = 0; iRow < spidTable.Rows.Count - 1; iRow++) 
                    { 
                        Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString();   //強行關閉用戶進程 
                        Cmd.ExecuteNonQuery(); 
                    } 
                } 
                conn.Close(); 
                conn.Dispose(); 
                //--------------------------------------------------------------------                 
                try 
                { 
                    string str = "use master restore database 數據庫名 from Disk='" + txtPath.Text.Trim() + "'"; 
                    SqlConnection conn1 = new SqlConnection(DateStr); 
                    conn1.Open(); 
                    SqlCommand cmd = new SqlCommand(str, conn1); 
                    cmd.ExecuteNonQuery(); 
                    if (MessageBox.Show("恢復成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) 
                    { 
                        this.Close(); 
                    } 
                } 
                catch (Exception ex) 
                { 
                    MessageBox.Show(ex.Message.ToString()); 
                } 
            } 
            else { 
                MessageBox.Show("請選擇備份文件位置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
            } 
        } 
       
        /// <summary> 
        /// 保存位置選擇按鈕事件 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        private void btn_open_Click(object sender, EventArgs e) 
        { 
            openFileDialog1.FilterIndex = 0; 
            openFileDialog1.FileName = ""; 
            openFileDialog1.Filter = "txt files (*.bak)|*.bak|All files (*.*)|*.*"; 
            if (openFileDialog1.ShowDialog() == DialogResult.OK) 
            { 
                txtPath.Text = openFileDialog1.FileName.ToString(); 
            } 
        } 
    } 
 } 

 

作者:fwj380891124

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved