欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的SQL备份与还原功能示例

程序员文章站 2023-12-01 11:34:46
本文实例讲述了c#实现的sql备份与还原功能。分享给大家供大家参考,具体如下: //记得加 folderbrowserdialog1 openfiledialog...

本文实例讲述了c#实现的sql备份与还原功能。分享给大家供大家参考,具体如下:

//记得加 folderbrowserdialog1 openfiledialog1 控件
using system.data.sqlclient; //连接数据库 公共变量
namespace windowsapplication1.goodmenhod
{
class getsqlconnection
{ 
string sql = "data source=win7-pc;database=kc;uid=sa;pwd=sa";
sqlconnection conn; 
public sqlconnection getcon()
{
conn = new sqlconnection(sql);
conn.open();
return conn;
}
}
}
using system.data.sqlclient;
using windowsapplication1.goodmenhod; //引用命名空间
namespace windowsapplication1
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
private void button1_click(object sender, eventargs e) //打开 备份路径
{
if (folderbrowserdialog1.showdialog() == dialogresult.ok)
{
txtpath.text = folderbrowserdialog1.selectedpath.tostring();
}
}
private void button2_click(object sender, eventargs e) //备份名称 保存 
{
try
{
if (txtpath.text != "" )
{
getsqlconnection gecon = new getsqlconnection();
sqlconnection con = gecon.getcon();
string strbacl = "backup database kc 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 button3_click(object sender, eventargs e) //打开 将要还原的文件
{
openfiledialog1.filterindex = 0;
openfiledialog1.filename = "";
openfiledialog1.filter = "txt files (*.bak)|*.bak|all files (*.*)|*.*";
if (openfiledialog1.showdialog() == dialogresult.ok)
{
textpaht.text = openfiledialog1.filename.tostring();
}
}
private void button4_click(object sender, eventargs e) //还原
{
if (textpaht.text != "")
{
getsqlconnection gecon = new getsqlconnection();
sqlconnection con = gecon.getcon();
if (con.state == connectionstate.open)
{
con.close();
}
//连接的数据库是master,所以要初始化新的连接字符串
string datestr = "data source=win7-pc;database=master;user id=sa;pwd=sa";
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( 'kc') ";//读取连接当前数据库的进程
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 kc to disk='" + textpaht.text.trim() + "' restore database kc 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);
}
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《c#窗体操作技巧汇总》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。