老D

我是一个粗人
  博客园  :: 首页  :: 新随笔  :: 管理

在代码中恢复sql server 数据库

Posted on 2007-08-25 13:57  老D  阅读(282)  评论(0编辑  收藏  举报

 

 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