c# 备份还原 例子

简介:
// 记得加 folderBrowserDialog1   openFileDialog1      控件
using  System.Data.SqlClient;    // 连接数据库   公共变量
namespace  WindowsApplication1.GoodMenhod
{
    
class  getSqlConnection
    { 
        
string  sql  =   " Data Source=win7-pc;database=Kc;uid=sa;pwd=sa " ;
        SqlConnection conn; 
      
        
public  SqlConnection GetCon()
        {
           conn 
=   new  SqlConnection(sql);
           conn.Open();
           
return  conn;
        }
     
    }
}


using  System.Data.SqlClient;
using  WindowsApplication1.GoodMenhod;   // 引用命名空间
namespace  WindowsApplication1
{
    
public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }
        
private   void  button1_Click( object  sender, EventArgs e)   // 打开 备份路径
        {
            
if  (folderBrowserDialog1.ShowDialog()  ==  DialogResult.OK)
            {
                txtPath.Text 
=  folderBrowserDialog1.SelectedPath.ToString();
            }
        }
        
private   void  button2_Click( object  sender, EventArgs e)   // 备份名称  保存 
        {
            
try
            {
                
if  (txtPath.Text  !=   ""  )
                {
                    getSqlConnection geCon 
=   new  getSqlConnection();
                    SqlConnection con 
=  geCon.GetCon();
                    
string  strBacl  =   " backup database Kc to disk=' "   +  txtPath.Text.Trim()  +   " \\ "   +  txtName.Text.Trim()  +   " .bak' " ;
                    SqlCommand Cmd 
=   new  SqlCommand(strBacl, con);
                    
if  (Cmd.ExecuteNonQuery()  !=   0 )
                    {
                        MessageBox.Show(
" 数据备份成功! " " 提示框 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
                        
this .Close();
                    }
                    
else
                    {
                        MessageBox.Show(
" 数据备份失败! " " 提示框 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                
else
                {
                    MessageBox.Show(
" 请填写备份的正确位置及文件名! " " 提示框 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
//  end 
            }
            
catch  (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }

        }
    }
}


   
private   void  button3_Click( object  sender, EventArgs e)   // 打开 将要还原的文件
        {
            openFileDialog1.FilterIndex 
=   0 ;
            openFileDialog1.FileName 
=   "" ;
            openFileDialog1.Filter 
=   " txt files (*.bak)|*.bak|All files (*.*)|*.* " ;

            
if  (openFileDialog1.ShowDialog()  ==  DialogResult.OK)
            {
                textPaht.Text 
=  openFileDialog1.FileName.ToString();
            }
        }
 
private   void  button4_Click( object  sender, EventArgs e)    // 还原
        {
             
if  (textPaht.Text  !=   "" )
            {
                getSqlConnection geCon 
=   new  getSqlConnection();
                SqlConnection con 
=  geCon.GetCon();
                
if  (con.State  ==  ConnectionState.Open)
                {
                    con.Close();
                }
                
// 连接的数据库是master,所以要初始化新的连接字符串
                 string  DateStr  =   " Data Source=win7-pc;Database=master;User id=sa;PWD=sa " ;
                SqlConnection conn 
=   new  SqlConnection(DateStr);
                conn.Open();
                
// -------------------杀掉所有连接 db_CSManage 数据库的进程--------------
               
//  string sql = " SELECT spid FROM master..sysprocesses WHERE dbid=db_id('" + strDBName + "')";
                 string  strSQL  =   " select spid from master..sysprocesses where dbid=db_id( 'Kc')  " ; // 读取连接当前数据库的进程
                SqlDataAdapter Da  =   new  SqlDataAdapter(strSQL, conn);
                DataTable spidTable 
=   new  DataTable();
                Da.Fill(spidTable);
                SqlCommand Cmd 
=   new  SqlCommand();
                Cmd.CommandType 
=  CommandType.Text;
                Cmd.Connection 
=  conn;
                
for  ( int  iRow  =   0 ; iRow  <=  spidTable.Rows.Count  -   1 ; iRow ++ )
                {
                    Cmd.CommandText 
=   " kill  "   +  spidTable.Rows[iRow][ 0 ].ToString();    // 强行关闭用户进程 
                    Cmd.ExecuteNonQuery();
                }
                conn.Close();
                conn.Dispose();
                
// --------------------------------------------------------------------
                SqlConnection sqlcon  =   new  SqlConnection(DateStr);
                sqlcon.Open();
                SqlCommand sqlCmd 
=   new  SqlCommand( " backup database Kc to disk=' "   +  textPaht.Text.Trim()  +   " ' restore database Kc from disk=' "   +  textPaht.Text.Trim()  +   " ' " , sqlcon);
                sqlCmd.ExecuteNonQuery();
                sqlCmd.Dispose();
                sqlcon.Close();
                sqlcon.Dispose();
                MessageBox.Show(
" 数据还原成功! " " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
                MessageBox.Show(
" 为了必免数据丢失,在数据库还原后将关闭整个系统。 " );
                Application.Exit();
            }
            
else
            {
                MessageBox.Show(
" 请选择备份文件! " " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }


    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2009/09/23/1572283.html,如需转载请自行联系原作者


相关文章
|
5G 文件存储
5G-GUTI详解
5G-GUTI(5G Globally Unique Temporary Identifier)是5G系统中全局唯一的临时UE标识,目的是提供在5G系统(5GS)中不泄露UE或用户永久身份的UE明确标识,提升安全性。它被用于接入、AMF和网络识别中,可以使用它在5GS中网络和UE之间的信令期间建立UE的身份。
2334 0
5G-GUTI详解
|
机器人 jenkins Java
jenkins pipeline流水线集成jacoco,sonar,robot framework,jmeter,fortify
jenkins pipeline流水线集成jacoco,sonar,robot framework,jmeter,fortify
1270 0
jenkins pipeline流水线集成jacoco,sonar,robot framework,jmeter,fortify
阿里云发票怎么开?刚开完,非常简单,分享给大家!
阿里云用户可在用户中心的发票管理页面开具电子或纸质发票。首次开票需设置发票抬头,支持个人或企业,可选增值税普通或专用发票。个人账号无法直接开企业发票,需变更实名认证。发票税率因产品而异,通常为6%或13%。发票抬头可修改,纸质发票邮寄费用视情况而定,电子发票同样具备法律效力。详情见阿里云帮助中心。
2185 0
|
机器学习/深度学习 监控 算法
深度学习在图像识别中的创新应用与未来趋势###
【10月更文挑战第14天】 本文探讨了深度学习技术在图像识别领域的创新突破,强调其在提升识别精度、效率及拓展应用场景上的关键作用。通过对比传统方法,凸显了深度学习模型的优越性,并展望其未来发展趋势,包括模型优化、跨模态学习及隐私保护等方向。 ###
337 0
|
人工智能
AIGC的出现对社会有啥影响
AIGC的出现对社会有啥影响
873 39
|
传感器 编解码 算法
[硬件选型] 工业相机之相机分类
[硬件选型] 工业相机之相机分类
433 0
|
JavaScript 调度
万界星空科技MES与WMS如何集成的?
传统制造业数字化转型正汹涌而来,要进一步提高产业发展质量,重塑制造业竞争优势,就必须加快发展数字化制造,加紧推动制造业的数字化转型。
313 0
|
机器学习/深度学习 Web App开发 算法
强化学习(Reinforcement Learning)
强化学习(Reinforcement Learning)是机器学习的一个分支,旨在让智能体(agent)通过与环境的交互学习如何做出决策以最大化累积奖励。在强化学习中,智能体通过试错的方式与环境进行交互,并根据环境的反馈(奖励或惩罚)调整自己的行为。
470 0
|
移动开发 安全 程序员
移动应用开发:Web App模式 、Native App模式及Hyprid App模式
移动应用开发:Web App模式 、Native App模式及Hyprid App模式
1220 0
|
存储 域名解析 监控