在代码中恢复sql server 数据库

 

 1  SQLDMO.SQLServer svr  =   new  SQLDMO.SQLServerClass();
 2              try
 3              {
 4                svr.Connect(Encrypt.UNDES("localhost","sa","password");
 5                SQLDMO.QueryResults qr = svr.EnumProcesses(-1);
 6                int iColPIDNum = -1;
 7                int iColDbName = -1;
 8                for (int i = 1; i <= qr.Columns; i++)
 9                {
10                    string strName = qr.get_ColumnName(i);
11                    if (strName.ToUpper().Trim() == "SPID")
12                    {
13                        iColPIDNum = i;
14                    }

15                    else if (strName.ToUpper().Trim() == "DBNAME")
16                    {
17                        iColDbName = i;
18                    }

19                    if (iColPIDNum != -1 && iColDbName != -1)
20                        break;
21                }

22
23                for (int i = 1; i <= qr.Rows; i++)
24                {
25                    int lPID = qr.GetColumnLong(i, iColPIDNum);
26                    string strDBName = qr.GetColumnString(i, iColDbName);
27                    if (strDBName.ToUpper() == DBName.ToUpper())
28                        svr.KillProcess(lPID);
29                }

30
31                SQLDMO.Restore res = new SQLDMO.RestoreClass();
32                res.Action = 0;
33                res.Files = ServiceFolder;
34                res.Database = DBName;
35                res.ReplaceDatabase = true;
36                res.SQLRestore(svr);
37            }

38              catch
39              {
40                throw;
41            }

42              finally
43              {
44                svr.DisConnect();
45            }
 
46

你可能感兴趣的:(SQL Server)