C#实现的SQL备份与还原功能示例
本文实例讲述了C#实现的SQL备份与还原功能。分享给大家供大家参考,具体如下:
//记得加folderBrowserDialog1openFileDialog1控件 usingSystem.Data.SqlClient;//连接数据库公共变量 namespaceWindowsApplication1.GoodMenhod { classgetSqlConnection { stringsql="DataSource=win7-pc;database=Kc;uid=sa;pwd=sa"; SqlConnectionconn; publicSqlConnectionGetCon() { conn=newSqlConnection(sql); conn.Open(); returnconn; } } } usingSystem.Data.SqlClient; usingWindowsApplication1.GoodMenhod;//引用命名空间 namespaceWindowsApplication1 { publicpartialclassForm1:Form { publicForm1() { InitializeComponent(); } privatevoidbutton1_Click(objectsender,EventArgse)//打开备份路径 { if(folderBrowserDialog1.ShowDialog()==DialogResult.OK) { txtPath.Text=folderBrowserDialog1.SelectedPath.ToString(); } } privatevoidbutton2_Click(objectsender,EventArgse)//备份名称保存 { try { if(txtPath.Text!="") { getSqlConnectiongeCon=newgetSqlConnection(); SqlConnectioncon=geCon.GetCon(); stringstrBacl="backupdatabaseKctodisk='"+txtPath.Text.Trim()+"\\"+txtName.Text.Trim()+".bak'"; SqlCommandCmd=newSqlCommand(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(Exceptionee) { MessageBox.Show(ee.Message.ToString()); } } } } privatevoidbutton3_Click(objectsender,EventArgse)//打开将要还原的文件 { openFileDialog1.FilterIndex=0; openFileDialog1.FileName=""; openFileDialog1.Filter="txtfiles(*.bak)|*.bak|Allfiles(*.*)|*.*"; if(openFileDialog1.ShowDialog()==DialogResult.OK) { textPaht.Text=openFileDialog1.FileName.ToString(); } } privatevoidbutton4_Click(objectsender,EventArgse)//还原 { if(textPaht.Text!="") { getSqlConnectiongeCon=newgetSqlConnection(); SqlConnectioncon=geCon.GetCon(); if(con.State==ConnectionState.Open) { con.Close(); } //连接的数据库是master,所以要初始化新的连接字符串 stringDateStr="DataSource=win7-pc;Database=master;Userid=sa;PWD=sa"; SqlConnectionconn=newSqlConnection(DateStr); conn.Open(); //-------------------杀掉所有连接db_CSManage数据库的进程-------------- //stringsql="SELECTspidFROMmaster..sysprocessesWHEREdbid=db_id('"+strDBName+"')"; stringstrSQL="selectspidfrommaster..sysprocesseswheredbid=db_id('Kc')";//读取连接当前数据库的进程 SqlDataAdapterDa=newSqlDataAdapter(strSQL,conn); DataTablespidTable=newDataTable(); Da.Fill(spidTable); SqlCommandCmd=newSqlCommand(); Cmd.CommandType=CommandType.Text; Cmd.Connection=conn; for(intiRow=0;iRow<=spidTable.Rows.Count-1;iRow++) { Cmd.CommandText="kill"+spidTable.Rows[iRow][0].ToString();//强行关闭用户进程 Cmd.ExecuteNonQuery(); } conn.Close(); conn.Dispose(); //-------------------------------------------------------------------- SqlConnectionsqlcon=newSqlConnection(DateStr); sqlcon.Open(); SqlCommandsqlCmd=newSqlCommand("backupdatabaseKctodisk='"+textPaht.Text.Trim()+"'restoredatabaseKcfromdisk='"+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); } }
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《C#窗体操作技巧汇总》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。