方法一(不使用sqldmo):
///
///备份方法
///
sqlconnection conn = new sqlconnection("server=.;database=master;user id=sa;password=sa;");
sqlcommand cmdbk = new sqlcommand();
cmdbk.commandtype = commandtype.text;
cmdbk.connection = conn;
cmdbk.commandtext = @"backup database test to disk='c:/ba' with init";
try
{
conn.open();
cmdbk.executenonquery();
messagebox.show("backup successed.");
}
catch(exception ex)
{
messagebox.show(ex.message);
}
finally
{
conn.close();
conn.dispose();
}
///
///还原方法
///
sqlconnection conn = new sqlconnection("server=.;database=master;user id=sa;password=sa;trusted_connection=false");
conn.open();
//kill database process
sqlcommand cmd = new sqlcommand("select spid from sysprocesses ,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.name='test'", conn);
sqldatareader dr;
dr = cmd.executereader();
arraylist list = new arraylist();
while(dr.read())
{
list.add(dr.getint16(0));
}
dr.close();
for(int i = 0; i < list.count; i++)
{
cmd = new sqlcommand(string.format("kill {0}", list[i]), conn);
cmd.executenonquery();
}
sqlcommand cmdrt = new sqlcommand();
cmdrt.commandtype = commandtype.text;
cmdrt.connection = conn;
cmdrt.commandtext = @"restore database test from disk='c:/ba'";
try
{
cmdrt.executenonquery();
messagebox.show("restore successed.");
}
catch(exception ex)
{
messagebox.show(ex.message);
}
finally
{
conn.close();
}
方法二(使用sqldmo):
///
///备份方法
///
sqldmo.backup backup = new sqldmo.backupclass();
sqldmo.sqlserver server = new sqldmo.sqlserverclass();
//显示进度条
sqldmo.backupsink_percentcompleteeventhandler progress = new sqldmo.backupsink_percentcompleteeventhandler(step);
backup.percentcomplete += progress;
try
{
server.loginsecure = false;
server.connect(".", "sa", "sa");
backup.action = sqldmo.sqldmo_backup_type.sqldmobackup_database;
backup.database = "test";
backup.files = @"d:/test/myprog/backuptest";
backup.backupsetname = "test";
backup.backupsetdescription = "backup the database of test";
backup.initialize = true;
backup.sqlbackup(server);
messagebox.show("backup successed.");
}
catch(exception ex)
{
messagebox.show(ex.message);
}
finally
{
server.disconnect();
}
this.pbdb.value = 0;
///
///还原方法
///
sqldmo.restore restore = new sqldmo.restoreclass();
sqldmo.sqlserver server = new sqldmo.sqlserverclass();
//显示进度条
sqldmo.restoresink_percentcompleteeventhandler progress = new sqldmo.restoresink_percentcompleteeventhandler(step);
restore.percentcomplete += progress;
//kill database process
sqlconnection conn = new sqlconnection("server=.;database=master;user id=sa;password=sa;trusted_connection=false");
conn.open();
sqlcommand cmd = new sqlcommand("select spid from sysprocesses ,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.name='test'", conn);
sqldatareader dr;
dr = cmd.executereader();
arraylist list = new arraylist();
while(dr.read())
{
list.add(dr.getint16(0));
}
dr.close();
for(int i = 0; i < list.count; i++)
{
cmd = new sqlcommand(string.format("kill {0}", list[i]), conn);
cmd.executenonquery();
}
conn.close();
try
{
server.loginsecure = false;
server.connect(".", "sa", "sa");
restore.action = sqldmo.sqldmo_restore_type.sqldmorestore_database;
restore.database = "test";
restore.files = @"d:/test/myprog/backuptest";
restore.filenumber = 1;
restore.replacedatabase = true;
restore.sqlrestore(server);
messagebox.show("restore successed.");
}
catch(exception ex)
{
messagebox.show(ex.message);
}
finally
{
server.disconnect();
}
this.pbdb.value = 0;
新闻热点
疑难解答