.net winform 学习笔记---数据库备份工具

private void ButtonBackUp_Click( object sender, System.EventArgs e)
        {
           
this .Cursor = Cursors.WaitCursor;
           
string _DBServ = Btnback1.Text;
           
string _DBName = Btnback2.Text;
           
string _DBUser = Btnback3.Text;
           
string _DBPass = Btnback4.Text;
           
           
string m_ConnectionStr   = " data source= " + _DBServ + " ;initial catalog=master;user id= " + _DBUser + " ;pwd= " + _DBPass + " ;Connect Timeout=5; " ;
            SqlConnection _Conn
= new SqlConnection(m_ConnectionStr);           
           
try
            {
                _Conn.Open();
                SaveFileDialog _SaveFileDialog
= new SaveFileDialog();         // 保存的文件目录路径      
                _SaveFileDialog.Title = " 请选择备份保存的目录 " ;
                _SaveFileDialog.Filter
= " (*.Bak)|*.Bak;|(All Files)|*.* " ;
               
if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
                {
                     
                   
string _FolderPath = _SaveFileDialog.FileName;                                          
                   
                    SqlCommand _Comm
= new SqlCommand( "" ,_Conn);
                   
// 执行数据库备份命令
                   
// _Comm.CommandText = "BACKUP DATABASE "+_DBName+" TO DISK = "+ _FolderPath +" ";
                    _Comm.CommandText = " BACKUP DATABASE " + _DBName + " TO DISK = ' " + _FolderPath + " ' " ;
                    _Comm.ExecuteNonQuery();                   
                   
this .Cursor = Cursors.Arrow;
                    _Conn.Close();
                    MessageBox.Show(
" 备份数据成功! " , " 提示 " ,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
               
this .Cursor = Cursors.Arrow;

            }
           
catch (System.Exception error)
            {
               
this .Cursor = Cursors.Arrow;
                MessageBox.Show(
" 异常: " + error.Message, " 提示 " ,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
           
this .Cursor = Cursors.Arrow;
        }

       
private void buttonXP1_Click( object sender, System.EventArgs e)
        {
            OpenFileDialog _OpenFileDialog
= new OpenFileDialog();
            _OpenFileDialog.Title
= " 请选择文件恢复 " ;
            _OpenFileDialog.Filter
= " (*.Bak)|*.Bak;|(All Files)|*.* " ;
           
if (_OpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                BtnRestory.Text
= _OpenFileDialog.FileName;
            }
        }

       
private void ButtonRestory_Click( object sender, System.EventArgs e)
        {
           
if (MessageBox.Show( " 该操作将数据覆盖!!/n/n如果选择[是],将原来的数据覆盖/n/n如果选择[否],将取消恢复 " , " 提示 " ,MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
               
return ;
           
if (MessageBox.Show( " 请再次确认,该操作不能恢复!!/n/n如果选择[是],将原来的数据覆盖/n/n如果选择[否],将取消恢复 " , " 提示 " ,MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.No)
               
return ;
           
this .Cursor = Cursors.WaitCursor;

           
string _DBServ = R1.Text;
           
string _DBName = R2.Text;
           
string _DBUser = R3.Text;
           
string _DBPass = R4.Text;

           
string m_ConnectionStr   = " data source= " + _DBServ + " ;initial catalog=master;user id= " + _DBUser + " ;pwd= " + _DBPass + " ;Connect Timeout=5; " ;
            SqlConnection _Conn
= new SqlConnection(m_ConnectionStr);           
           
try
            {
                _Conn.Open(); 
               
// SqlCommand _Comm = new SqlCommand("restore filelistonly from disk='"+_FolderPath+@"/"+_DBName+@".bak'",_Conn); // 获得原来的逻辑名称以及物理路径
                string _FolderPath = " d: " ;
               
string _DBPath = " ces " ;
                SqlCommand _Comm
= new SqlCommand( " restore filelistonly from disk=' " + _FolderPath + @" / " + _DBPath + @" .bak' " ,_Conn); // 获得原来的逻辑名称以及物理路径
                SqlDataReader _Reader = null ;
                _Reader
= _Comm.ExecuteReader();
               
string []  _LogName = new string [ 2 ]; // 获得逻辑名称
                string []  _PhiPath = new string [ 2 ]; // 获得物理路径
                int j = 0 ;
               
while (_Reader.Read())
                {
                    _LogName[j]
= ( string )_Reader.GetValue( 0 );
                    _PhiPath[j]
= ( string )_Reader.GetValue( 1 );
                    j
= j + 1 ;
                }
                _Reader.Close();
                MessageBox.Show(_LogName[
0 ]);
                       
               
// 执行数据库恢复脚本
                _Comm = new SqlCommand( "" ,_Conn);
                _Comm.CommandText
= " RESTORE DATABASE [ " + _DBName + " ]   FROM DISK = ' " + BtnRestory.Text + " ' With Move ' " + _LogName[ 0 ] +
                   
" ' TO ' " + _PhiPath[ 0 ] + " ',  Move ' " + _LogName[ 1 ] + " ' TO ' " + _PhiPath[ 1 ] + " ' " ;
                _Comm.ExecuteNonQuery();                   
                           
               
this .Cursor = Cursors.Arrow;
                MessageBox.Show(
" 数据恢复成功! " , " 提示 " ,MessageBoxButtons.OK,MessageBoxIcon.Information);
                _Conn.Close();
                      
               
this .Cursor = Cursors.Arrow;

            }
           
catch (System.Exception error)
            {
               
this .Cursor = Cursors.Arrow;
                MessageBox.Show(
" 异常: " + error.Message, " 错误 " ,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值