在VB.net中实现sql数据库的备份与恢复

作者:网络 来源:佚名 更新时间:2008-11-13 23:23:38 点击:

private sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click '备份数据库 'backup database test to disk='c:\backup' with format dim cn as sqlconnection dim cm as sqlcommand cn = getdb() cm = new sqlcommand("backup database jxbase to disk='c:\backup ' with format", cn) cn.open() cm.executenonquery() msgbox("备份成功") cn.close()

end sub

'恢复 private sub button6_click(byval sender as system.object, byval e as system.eventargs) handles button6.click dim cn as new sqlconnection dim cn1 as new sqlconnection dim mydr as sqldatareader dim str as string

 dim tmpconnectionstring as string = "data source=jjk;initial catalog=master;integrated security=true" cn.connectionstring = tmpconnectionstring cn1.connectionstring = tmpconnectionstring cn.open() cn1.open() dim cm as sqlcommand = new sqlcommand("select spid from master..sysprocesses where dbid=db_id('jxbase')", cn) mydr = cm.executereader()

dim cm1 as sqlcommand = new sqlcommand() cm1.connection = cn1

while (mydr.read()) '杀死进程

str = "kill " & mydr("spid").tostring() cm1.commandtext = str cm1.commandtype = commandtype.text application.doevents() cm1.executenonquery() '杀进程 end while mydr.close() cm = new sqlcommand("restore database jxbase from disk='c:\backup'", cn) '恢复备份 cm.executenonquery() msgbox("恢复成功成功") cn.close() cn1.close()

end sub

最大的网站源码资源下载站,