• asp.net还原备份数据库(C#)


    因为做项目的时候用到对数据库的还原和备份,第一次接触,所以上网查了关于这方面的资料,网络果然是个好东西,该有的都有了,这里我就把原文中的代码直接粘贴过来了。

    [html] view plain copy
     
    1. using System;  
    2.   
    3. using System.Configuration;  
    4.   
    5. using System.Data.SqlClient;  
    6.   
    7. using System.Data;  
    8.   
    9. namespace web.base_class  
    10.   
    11. {  
    12.   
    13. /// <summary>  
    14.   
    15. /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复  
    16.   
    17. /// </summary>  
    18.   
    19. public class DbOper  
    20.   
    21. {  
    22.   
    23. private string server;  
    24.   
    25. private string uid;  
    26.   
    27. private string pwd;  
    28.   
    29. private string database;  
    30.   
    31. private string conn;  
    32.   
    33. /// <summary>  
    34.   
    35. /// DbOper类的构造函数  
    36.   
    37. /// </summary>  
    38.   
    39. public DbOper()  
    40.   
    41. {  
    42.   
    43. conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();  
    44.   
    45. server=cut(conn,"server=",";");  
    46.   
    47. uid=cut(conn,"uid=",";");  
    48.   
    49. pwd=cut(conn,"pwd=",";");  
    50.   
    51. database=cut(conn,"database=",";");  
    52.   
    53. }  
    54.   
    55. public string cut(string str,string bg,string ed)  
    56.   
    57. {  
    58.   
    59. string sub;  
    60.   
    61. sub=str.Substring(str.IndexOf(bg)+bg.Length);  
    62.   
    63. sub=sub.Substring(0,sub.IndexOf(";"));  
    64.   
    65. return sub;  
    66.   
    67. }  
    68.   
    69.   
    70.   
    71. /// <summary>  
    72.   
    73. /// 数据库备份  
    74.   
    75. /// </summary>  
    76.   
    77. public bool DbBackup(string url)  
    78.   
    79. {  
    80.   
    81. SQLDMO.Backup oBackup = new SQLDMO.BackupClass();  
    82.   
    83. SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();  
    84.   
    85. try  
    86.   
    87. {  
    88.   
    89. oSQLServer.LoginSecure = false;  
    90.   
    91. oSQLServer.Connect(server,uid, pwd);  
    92.   
    93. oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;  
    94.   
    95. oBackup.Database = database;  
    96.   
    97. oBackup.Files = url;//"d:Northwind.bak";  
    98.   
    99. oBackup.BackupSetName = database;  
    100.   
    101. oBackup.BackupSetDescription = "数据库备份";  
    102.   
    103. oBackup.Initialize = true;  
    104.   
    105. oBackup.SQLBackup(oSQLServer);  
    106.   
    107. return true;  
    108.   
    109. }  
    110.   
    111. catch  
    112.   
    113. {  
    114.   
    115. return false;  
    116.   
    117. throw;  
    118.   
    119. }  
    120.   
    121. finally  
    122.   
    123. {  
    124.   
    125. oSQLServer.DisConnect();  
    126.   
    127. }  
    128.   
    129. }  
    130.   
    131.   
    132.   
    133. /// <summary>  
    134.   
    135. /// 数据库恢复  
    136.   
    137. /// </summary>  
    138.   
    139. public string DbRestore(string url)  
    140.   
    141. {  
    142.   
    143. if(exepro()!=true)//执行存储过程  
    144.   
    145. {  
    146.   
    147. return "操作失败";  
    148.   
    149. }  
    150.   
    151. else  
    152.   
    153. {  
    154.   
    155. SQLDMO.Restore orestore = new SQLDMO.RestoreClass();  
    156.   
    157. SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();  
    158.   
    159. try  
    160.   
    161. {  
    162.   
    163. oSQLServer.LoginSecure = false;  
    164.   
    165. oSQLServer.Connect(server, uid, pwd);  
    166.   
    167. orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;  
    168.   
    169. orestore.Database = database;  
    170.   
    171. orestore.Files = url;//@"d:Northwind.bak";  
    172.   
    173. orestore.FileNumber = 1;  
    174.   
    175. orestore.ReplaceDatabase = true;  
    176.   
    177. orestore.SQLRestore(oSQLServer);  
    178.   
    179. return "ok";  
    180.   
    181. }  
    182.   
    183. catch(Exception e)  
    184.   
    185. {  
    186.   
    187. return "恢复数据库失败";  
    188.   
    189. throw;  
    190.   
    191. }  
    192.   
    193. finally  
    194.   
    195. {  
    196.   
    197. oSQLServer.DisConnect();  
    198.   
    199. }  
    200.   
    201. }  
    202.   
    203. }  
    204.   
    205. private bool exepro()  
    206.   
    207. {  
    208.   
    209. SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");  
    210.   
    211. SqlCommand cmd = new SqlCommand("killspid",conn1);  
    212.   
    213. cmd.CommandType = CommandType.StoredProcedure;  
    214.   
    215. cmd.Parameters.Add("@dbname","port");  
    216.   
    217. try  
    218.   
    219. {  
    220.   
    221. conn1.Open();  
    222.   
    223. cmd.ExecuteNonQuery();  
    224.   
    225. return true;  
    226.   
    227. }  
    228.   
    229. catch(Exception ex)  
    230.   
    231. {  
    232.   
    233. return false;  
    234.   
    235. }  
    236.   
    237. finally  
    238.   
    239. {  
    240.   
    241. conn1.Close();  
    242.   
    243. }  
    244.   
    245.   
    246.   
    247. }  
    248.   
    249. }  
    250.   
    251.   
    252. }  


     

    需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:

    [html] view plain copy
     
    1. create proc killspid (@dbname varchar(20))  
    2. as  
    3. begin  
    4. declare @sql nvarchar(500)  
    5. declare @spid int  
    6. set @sql='declare getspid cursor for   
    7. select spid from sysprocesses where dbid=db_id('''+@dbname+''')'  
    8. exec (@sql)  
    9. open getspid  
    10. fetch next from getspid into @spid  
    11. while @@fetch_status<>-1  
    12. begin  
    13. exec('kill '+@spid)  
    14. fetch next from getspid into @spid  
    15. end  
    16. close getspid  
    17. deallocate getspid  
    18. end  
    19. GO  


     

    在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。

    但是,当我执行的时候发现一个问题,就是sqlDMO无法添加引用。首先我们了解一下什么是sqlDmo

    SQLDMO.dll是随SQL Server2000一起发布的。SQLDMO.dll自身是一个COM对象SQLDMO(SQLDistributed Management Objects,SQL分布式管理对象)封装Microsoft SQL Server 2000 数据库中的对象。SQL-DMO 允许用支持自动化或 COM的语言编写应用程序,以管理 SQL Server 安装的所有部分。SQL-DMO 是 SQL Server 2000 中的 SQL Server 企业管理器所使用的应用程序接口 (API);因此使用 SQL-DMO 的应用程序可以执行 SQL Server 企业管理器执行的所有功能。

    我们在这里用到它的备份和恢复功能!

    但是,这里遇到一个问题,就是如何引用sqlDMOD 问题,网上的答案有很多,大概可以分为这么几类:

    1、  如果安装SQLSERVER的话,点击添加引用,选中com,然后找到SQLDOM,选中确定。

    2、  在.NET项目的解决方案中,单击右键,选择“添加引用”,将弹出“添加引用”对话框,选中浏览,浏览“C:Program FilesMicrosoft SQL Server80ToolsBinn”。找到SQLDMO.DLL的存放路径,然后单击“确定”即可。

    3、  第一步:首先将msvcr71.dll,  SQLDMO.DLL,Resources2052sqldmo.rll,Resources1033sqldmo.rll 拷贝到C:ProgramFilesMicrosoft SQL Server80ToolsBinn目录。

    第二步:打开开始,在运行中输入 regsvr32 "C:Program FilesMicrosoft SQLServer80ToolsBinnsqldmo.dll" 注册sqldmo.dll。

    也许是跟配置有关,我电脑删安装了数据库,但是找不到SQLDOM,第二种方法在binn下也没有找到相应的文件,第三种下载了文件,但是注册不成功。就这样折腾了很长时间。不过功夫不负有心人,最终找到解决的办法了。

    第一步:首先将msvcr71.dll,  SQLDMO.DLL,Resources2052sqldmo.rll,Resources1033sqldmo.rll 拷贝到C:ProgramFilesMicrosoft SQL Server80ToolsBinn目录。

    第二步:打开开始,在运行中输入 regsvr32 "C:Program FilesMicrosoft SQLServer80ToolsBinnsqldmo.dll" 注册sqldmo.dll。

    正常情况下,经过以上两个步骤,网页就应该可以访问了的。但是我们经过以上两次操作后,访问网页依然提示如下错误:

    Retrieving the COM class factory forcomponent with CLSID {10020200-E260-11CF-AE68-00AA004A34D5} failed due to thefollowing error: 80070005.后经过一段时间的检查,我们发现C:Program Files文件夹仅有Administrator和System的控制权限,而没有其他任何用户的权限,因此我们为Microsoft SQL Server文件夹增加上Network Service 的读取权限。

    至此,问题得到解决!

    程序代码引用自:http://www.cnblogs.com/zgqys1980/archive/2008/09/25/1298787.html

    注册sqldom下载资源来自:http://download.csdn.net/detail/kangcooi/4896546

    如果需要下载程序(我是用VS2012实现的)以及需要注册的dll请点击http://download.csdn.net/detail/laner0515/5065686

    我已经封装好并且可执行。

  • 相关阅读:
    Android Sensor Test
    [转]Android重力感应开发
    nexus5 root教程
    C# split字符串 依据1个或多个空格
    leetcode
    [ffmpeg 扩展第三方库编译系列] 关于须要用到cmake 创建 mingw32编译环境问题
    JAVA网络爬虫WebCollector深度解析——爬虫内核
    Apache htaccess 重写假设文件存在!
    javascript --- 事件托付
    LeetCode——Populating Next Right Pointers in Each Node II
  • 原文地址:https://www.cnblogs.com/zxtceq/p/5898806.html
Copyright © 2020-2023  润新知