private void button1_Click(object sender, EventArgs e) { try { if (txtPath.Text != "" && txtName122.Text != "") { getSqlConnection geCon = new getSqlConnection(); SqlConnection con = geCon.GetCon(); string strBacl = "backup database db_CSManage 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 btn_backup_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; if (txtPath.Text != "") { getSqlConnection geCon = new getSqlConnection(); SqlConnection con = geCon.GetCon(); string strBacl = "backup database TJ_Tutors_System 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); } 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 bntOk_Click(object sender, EventArgs e) { if (textPaht.Text != "") { getSqlConnection geCon = new getSqlConnection(); SqlConnection con = geCon.GetCon(); if (con.State == ConnectionState.Open) { con.Close(); } string DateStr = "Data Source=mrwxk\\wangxiaoke;Database=master;User id=sa;PWD="; SqlConnection conn = new SqlConnection(DateStr); conn.Open(); //-------------------杀掉所有连接 db_CSManage 数据库的进程-------------- string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'db_CSManage') "; 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 log db_CSManage to disk='" + textPaht.Text.Trim() + "' restore database db_CSManage 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); } }
private void btn_return_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; if (textPaht.Text != "") { getSqlConnection geCon = new getSqlConnection(); SqlConnection con = geCon.GetCon(); if (con.State == ConnectionState.Open) { con.Close(); } //连接的数据库是master,所以要初始化新的连接字符串 string DateStr = "Data Source=.;Initial Catalog=master;Integrated Security=True"; 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( 'TJ_Tutors_System') ";//读取连接当前数据库的进程 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 TJ_Tutors_System to disk='" + textPaht.Text.Trim() + "' restore database TJ_Tutors_System from disk='" + textPaht.Text.Trim() + "' WITH REPLACE", 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); } }