如何用SQLDMO在ASP.NET页面下实现数据库的备份与恢复!

完整的操作类如下:
  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.Data;
  4 None.gif using  System.Data.SqlClient;
  5 None.gif
  6 None.gif namespace  DbBackUp
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  9InBlock.gif    /// 创建人:Terrylee
 10InBlock.gif    /// 创建时间:2005年8月1日
 11InBlock.gif    /// 功能描述:实现数据库的备份和还原
 12InBlock.gif    /// 更新记录:
 13ExpandedSubBlockEnd.gif    /// </summary>

 14InBlock.gif    public class DbOperate
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{    
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 17InBlock.gif        /// 服务器
 18ExpandedSubBlockEnd.gif        /// </summary>

 19InBlock.gif        private string server;
 20InBlock.gif        
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 22InBlock.gif        /// 登录名
 23ExpandedSubBlockEnd.gif        /// </summary>

 24InBlock.gif        private string uid;
 25InBlock.gif        
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 27InBlock.gif        /// 登录密码
 28ExpandedSubBlockEnd.gif        /// </summary>

 29InBlock.gif        private string pwd;
 30InBlock.gif        
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 32InBlock.gif        /// 要操作的数据库
 33ExpandedSubBlockEnd.gif        /// </summary>

 34InBlock.gif        private string database;
 35InBlock.gif        
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 37InBlock.gif        /// 数据库连接字符串
 38ExpandedSubBlockEnd.gif        /// </summary>

 39InBlock.gif        private string conn;
 40InBlock.gif
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42InBlock.gif        /// DbOperate类的构造函数
 43InBlock.gif        /// 在这里进行字符串的切割,获取服务器,登录名,密码,数据库
 44ExpandedSubBlockEnd.gif        /// </summary>

 45InBlock.gif        public DbOperate()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            conn = System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
 48InBlock.gif            server = StringCut(conn,"server=",";");
 49InBlock.gif            uid = StringCut(conn,"uid=",";");
 50InBlock.gif            pwd = StringCut(conn,"pwd=",";");
 51InBlock.gif            database = StringCut(conn,"database=",";");
 52ExpandedSubBlockEnd.gif        }

 53InBlock.gif        
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 55InBlock.gif        /// 切割字符串
 56InBlock.gif        /// </summary>
 57InBlock.gif        /// <param name="str"></param>
 58InBlock.gif        /// <param name="bg"></param>
 59InBlock.gif        /// <param name="ed"></param>
 60ExpandedSubBlockEnd.gif        /// <returns></returns>

 61InBlock.gif        public string StringCut(string str,string bg,string ed)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif            string sub;
 64InBlock.gif            sub=str.Substring(str.IndexOf(bg)+bg.Length);
 65InBlock.gif            sub=sub.Substring(0,sub.IndexOf(";"));
 66InBlock.gif            return sub;
 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif        
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 70InBlock.gif        /// 构造文件名
 71InBlock.gif        /// </summary>
 72ExpandedSubBlockEnd.gif        /// <returns>文件名</returns>

 73InBlock.gif        private string CreatePath()
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            string CurrTime = System.DateTime.Now.ToString();
 76InBlock.gif            CurrTime = CurrTime.Replace("-","");
 77InBlock.gif            CurrTime = CurrTime.Replace(":","");
 78InBlock.gif            CurrTime = CurrTime.Replace(" ","");
 79InBlock.gif            CurrTime = CurrTime.Substring(0,12);
 80InBlock.gif            string path = @"d:\\aaa\\";
 81InBlock.gif            path += database;
 82InBlock.gif            path += "_db_";
 83InBlock.gif            path += CurrTime;
 84InBlock.gif            path += ".BAK";
 85InBlock.gif            return path;
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 89InBlock.gif        /// 数据库备份
 90InBlock.gif        /// </summary>
 91ExpandedSubBlockEnd.gif        /// <returns>备份是否成功</returns>

 92InBlock.gif        public bool DbBackup()
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            string path = CreatePath();
 95InBlock.gif            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
 96InBlock.gif            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
 97InBlock.gif            try
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 99InBlock.gif                oSQLServer.LoginSecure = false;
100InBlock.gif                oSQLServer.Connect(server,uid, pwd);
101InBlock.gif                oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
102InBlock.gif                oBackup.Database = database;
103InBlock.gif                oBackup.Files = path;
104InBlock.gif                oBackup.BackupSetName = database;
105InBlock.gif                oBackup.BackupSetDescription = "数据库备份";
106InBlock.gif                oBackup.Initialize = true;
107InBlock.gif                oBackup.SQLBackup(oSQLServer);
108InBlock.gif
109InBlock.gif                return true;
110ExpandedSubBlockEnd.gif            }

111InBlock.gif            catch(Exception ex)
112ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
113InBlock.gif                return false;
114InBlock.gif                throw ex;
115ExpandedSubBlockEnd.gif            }

116InBlock.gif            finally
117ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
118InBlock.gif                oSQLServer.DisConnect();
119ExpandedSubBlockEnd.gif            }

120ExpandedSubBlockEnd.gif        }

121InBlock.gif
122ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
123InBlock.gif        /// 数据库恢复
124ExpandedSubBlockEnd.gif        /// </summary>

125InBlock.gif        public string DbRestore()
126ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
127InBlock.gif            if(exepro()!=true)//执行存储过程
128ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
129InBlock.gif                return "操作失败";
130ExpandedSubBlockEnd.gif            }

131InBlock.gif            else
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
133InBlock.gif                SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
134InBlock.gif                SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
135InBlock.gif                try
136ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
137InBlock.gif                    exepro();
138InBlock.gif                    oSQLServer.LoginSecure = false;
139InBlock.gif                    oSQLServer.Connect(server, uid, pwd);
140InBlock.gif                    oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
141InBlock.gif                    oRestore.Database = database;
142ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////自行修改
143InBlock.gif                    oRestore.Files = @"d:\aaa\aaa.bak";
144InBlock.gif                    oRestore.FileNumber = 1;
145InBlock.gif                    oRestore.ReplaceDatabase = true;
146InBlock.gif                    oRestore.SQLRestore(oSQLServer);
147InBlock.gif
148InBlock.gif                    return "ok";
149ExpandedSubBlockEnd.gif                }

150InBlock.gif                catch(Exception e)
151ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
152InBlock.gif                    return "恢复数据库失败";
153InBlock.gif                    throw e;
154ExpandedSubBlockEnd.gif                }

155InBlock.gif                finally
156ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
157InBlock.gif                    oSQLServer.DisConnect();
158ExpandedSubBlockEnd.gif                }

159ExpandedSubBlockEnd.gif            }

160ExpandedSubBlockEnd.gif        }

161InBlock.gif        
162ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
163InBlock.gif        /// 杀死当前库的所有进程
164InBlock.gif        /// </summary>
165ExpandedSubBlockEnd.gif        /// <returns></returns>

166InBlock.gif        private bool exepro()
167ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
168InBlock.gif
169InBlock.gif            SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");
170InBlock.gif            SqlCommand cmd = new SqlCommand("killspid",conn1);
171InBlock.gif            cmd.CommandType = CommandType.StoredProcedure;
172InBlock.gif            cmd.Parameters.Add("@dbname","aaa");
173InBlock.gif            try
174ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
175InBlock.gif                conn1.Open();
176InBlock.gif                cmd.ExecuteNonQuery();
177InBlock.gif                return true;
178ExpandedSubBlockEnd.gif            }

179InBlock.gif            catch(Exception ex)
180ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
181InBlock.gif                return false;
182ExpandedSubBlockEnd.gif            }

183InBlock.gif            finally
184ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
185InBlock.gif                conn1.Close();
186ExpandedSubBlockEnd.gif            }

187ExpandedSubBlockEnd.gif        }

188InBlock.gif
189ExpandedSubBlockEnd.gif    }

190InBlock.gif
191ExpandedBlockEnd.gif}

192 None.gif

转载于:https://www.cnblogs.com/xiazhi33/articles/866573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值