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);
}
}
{
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);
}
}