| 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); |
| } |
| } |
| 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数据库恢复后台代码:
| 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) |
| { |
| |
| |
| |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| |
| private void btn_ok_Click(object sender, EventArgs e) |
| { |
| if (this.txtPath.Text != ""&&this.txtPath.Text != null) |
| { |
| SqlConnection conn = new SqlConnection(DateStr); |
| conn.Open(); |
| |
| 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); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| 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(); |
| } |
| } |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?