public classSqlServerBackup
{private stringdatabase;private stringserver;private stringuid;private stringpwd;public stringDatabase
{get{return this.database;
}set{this.database =value;
}
}public stringServer
{get{return this.server;
}set{this.server =value;
}
}public stringPwd
{get{return this.pwd;
}set{this.pwd =value;
}
}public stringUid
{get{return this.uid;
}set{this.uid =value;
}
}public bool DbBackup(stringurl)
{
Backup oBackup= newBackupClass();
SQLServer oSQLServer= newSQLServerClass();boolresult;try{
oSQLServer.LoginSecure= false;
oSQLServer.Connect(this.server, this.uid, this.pwd);
oBackup.Action=SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database= this.database;
oBackup.Files=url;
oBackup.BackupSetName= this.database;
oBackup.BackupSetDescription= "数据库备份";
oBackup.Initialize= true;
oBackup.SQLBackup(oSQLServer);
result= true;
}catch{
result= false;
}finally{
oSQLServer.DisConnect();
}returnresult;
}public bool DbRestore(stringurl)
{boolresult;if (!this.exepro())
{
result= false;
}else{
Restore oRestore= newRestoreClass();
SQLServer oSQLServer= newSQLServerClass();try{
oSQLServer.LoginSecure= false;
oSQLServer.Connect(this.server, this.uid, this.pwd);
oRestore.Action=SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database= this.database;
oRestore.Files=url;
oRestore.FileNumber= 1;
oRestore.ReplaceDatabase= true;
oRestore.SQLRestore(oSQLServer);
result= true;
}catch{
result= false;
}finally{
oSQLServer.DisConnect();
}
}returnresult;
}private boolexepro()
{
SqlConnection conn= new SqlConnection(string.Concat(new string[]
{"server=",this.server,";uid=",this.uid,";pwd=",this.pwd,";database=master"}));
SqlCommand cmd= new SqlCommand("killspid", conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@dbname", this.database);boolresult;try{
conn.Open();
cmd.ExecuteNonQuery();
result= true;
}catch{
result= false;
}finally{
conn.Close();
}returnresult;
}
}