Forráskód Böngészése

添加Mysql、SqlServer数据库备份、还原接口

世龙 苏 7 éve
szülő
commit
e93d5fc8eb

+ 8 - 0
CommonLibrary/CommonLibrary.csproj

@@ -34,6 +34,13 @@
     <Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
     </Reference>
+    <Reference Include="MySql.Data, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
+      <HintPath>..\packages\MySqlBackup.NET.2.0.12\lib\net45\MySql.Data.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="MySqlBackup, Version=2.0.12.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\MySqlBackup.NET.2.0.12\lib\net45\MySqlBackup.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.ComponentModel.DataAnnotations" />
     <Reference Include="System.Core" />
@@ -64,6 +71,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="ComputerUtil.cs" />
+    <Compile Include="DatabaseUtil.cs" />
     <Compile Include="DateUtil.cs" />
     <Compile Include="HardDiskInfo.cs" />
     <Compile Include="IpAddressAttribute.cs" />

+ 195 - 0
CommonLibrary/DatabaseUtil.cs

@@ -0,0 +1,195 @@
+namespace CommonLibrary
+{
+    using System;
+    using System.Data.SqlClient;
+    using MySql.Data.MySqlClient;
+
+    public enum DBTYPE
+    {
+        Mysql,
+        Sqlserver,
+    }
+
+    public static class DatabaseUtil
+    {
+        /// TODO:需要进行测试
+        /// <summary>
+        /// 备份、还原数据库的接口
+        /// </summary>
+        /// <param name="type">Mysql、Sql Sever数据库</param>
+        /// <param name="blBackOrRestore">true:备份;false:还原</param>
+        /// <param name="connectionString">连接数据库字符串</param>
+        /// <param name="filePath">备份或还原的路径</param>
+        /// <returns>是否成功</returns>
+        public static bool Action(DBTYPE type, bool blBackOrRestore, string connectionString, string filePath)
+        {
+            if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(filePath))
+            {
+                return false;
+            }
+
+            if (blBackOrRestore)
+            {
+                if (type == DBTYPE.Mysql)
+                {
+                    return MysqlBackup(connectionString, filePath);
+                }
+                else if (type == DBTYPE.Sqlserver)
+                {
+                    return SqlserverBackup(connectionString, filePath);
+                }
+            }
+            else
+            {
+                if (type == DBTYPE.Mysql)
+                {
+                    return MysqlRestore(connectionString, filePath);
+                }
+                else if (type == DBTYPE.Sqlserver)
+                {
+                    return SqlserverRestore(connectionString, filePath);
+                }
+            }
+
+            return true;
+        }
+
+        private static bool MysqlBackup(string connectionString, string filePath)
+        {
+            if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(filePath))
+            {
+                return false;
+            }
+
+            try
+            {
+                using (MySqlConnection conn = new MySqlConnection(filePath))
+                {
+                    using (MySqlCommand cmd = new MySqlCommand())
+                    {
+                        using (MySqlBackup mb = new MySqlBackup(cmd))
+                        {
+                            cmd.Connection = conn;
+                            conn.Open();
+                            mb.ExportToFile(filePath);
+                            conn.Close();
+                        }
+                    }
+                }
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        private static bool MysqlRestore(string connectionString, string filePath)
+        {
+            if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(filePath))
+            {
+                return false;
+            }
+
+            try
+            {
+                using (MySqlConnection conn = new MySqlConnection(connectionString))
+                {
+                    using (MySqlCommand cmd = new MySqlCommand())
+                    {
+                        using (MySqlBackup mb = new MySqlBackup(cmd))
+                        {
+                            cmd.Connection = conn;
+                            conn.Open();
+                            mb.ImportFromFile(filePath);
+                            conn.Close();
+                        }
+                    }
+                }
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        private static string Cut(string str, string bg, string ed)
+        {
+            string sub = str.Substring(str.IndexOf(bg) + bg.Length);
+            sub = sub.Substring(0, sub.IndexOf(";"));
+
+            return sub;
+        }
+
+        private static bool SqlserverBackup(string connectionString, string filePath)
+        {
+            if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(filePath))
+            {
+                return false;
+            }
+
+            try
+            {
+                string database = Cut(connectionString, "database=", ";");
+
+                string sql = "BACKUP DATABASE " + database + " to DISK = '" + filePath + "'";
+
+                using (SqlConnection conn = new SqlConnection(connectionString))
+                {
+                    conn.Open();
+                    using (SqlCommand cmd = new SqlCommand(sql, conn))
+                    {
+                        cmd.ExecuteNonQuery();
+                        conn.Close();
+                    }
+                }
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        private static bool SqlserverRestore(string connectionString, string filePath)
+        {
+            if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(filePath))
+            {
+                return false;
+            }
+
+            try
+            {
+                string database = Cut(connectionString, "database=", ";");
+
+                using (SqlConnection conn = new SqlConnection(connectionString))
+                {
+                    conn.Open();
+                    string sqlStmt2 = string.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
+                    SqlCommand bu2 = new SqlCommand(sqlStmt2, conn);
+                    bu2.ExecuteNonQuery();
+
+                    string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + filePath + "'WITH REPLACE;";
+                    SqlCommand bu3 = new SqlCommand(sqlStmt3, conn);
+                    bu3.ExecuteNonQuery();
+
+                    string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
+                    SqlCommand bu4 = new SqlCommand(sqlStmt4, conn);
+                    bu4.ExecuteNonQuery();
+
+                    conn.Close();
+                }
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            return true;
+        }
+    }
+}

+ 1 - 0
CommonLibrary/packages.config

@@ -4,5 +4,6 @@
   <package id="Microsoft.AspNet.Razor" version="3.2.6" targetFramework="net45" />
   <package id="Microsoft.AspNet.WebPages" version="3.2.6" targetFramework="net45" />
   <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
+  <package id="MySqlBackup.NET" version="2.0.12" targetFramework="net45" />
   <package id="StyleCop.Analyzers" version="1.0.2" targetFramework="net45" developmentDependency="true" />
 </packages>