日期:2014-05-18 浏览次数:20412 次
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();//创建还原对象 SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();//创建服务对象 try { oSQLServer.LoginSecure = false; oSQLServer.Connect("localhost\\sqlexpress", "sa", "sa");//连接数据服务 SQLDMO.QueryResults qr = oSQLServer.EnumProcesses(-1);//查询该数据库正在连接的进程 int iColPIDNum = -1; int iColDbName = -1; for (int i = 1; i <= qr.Columns; i++) { string strName = qr.get_ColumnName(i); if (strName.ToUpper().Trim() == "SPID") { iColPIDNum = i; } else if (strName.ToUpper().Trim() == "DBNAME") { iColDbName = i; } if (iColPIDNum != -1 && iColDbName != -1) break; } for (int i = 1; i <= qr.Rows; i++) { int IPID = qr.GetColumnLong(i, iColPIDNum); string strDbName = qr.GetColumnString(i, iColDbName); if (strDbName.ToUpper().Trim() == "mm_gis_intmap".ToUpper()) { oSQLServer.KillProcess(IPID);//kill正在连接的进程 } } oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; oRestore.Database = "mm_gis_intmap"; oRestore.Files = @"C:\mm_gis_intmap\mmgt_t.bak";//设置数据备份文件,如果该文件是完全备份,运行OK,差异备份即不行 //oRestore.StandbyFiles = @"C:\mm_gis_intmap\mmgt_t1.bak"; oRestore.FileNumber = 1; oRestore.ReplaceDatabase = true; oRestore.SQLRestore(oSQLServer); } catch { throw; } finally { oSQLServer.DisConnect(); }