/**********************************************************************************
*
* 功能說明:備份和恢復SQL Server數據庫
* 作者: 劉功勳;
* 版本:V0.1(C#2.0);時間:2007-1-1
* 當使用SQL Server時,請引用 COM組件中的,SQLDMO.dll組件
* 當使用Access中,請浏覽添加引用以下兩個dll
* 引用C:\Program Files\Common Files\System\ado\msadox.dll,該DLL包含ADOX命名空間
* 引用C:\Program Files\Common Files\System\ado\msjro.dll,該DLL包含JRO命名空間
* *******************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ADOX;//該命名空間包含創建ACCESS的類(方法)--解決方案 ==> 引用 ==> 添加引用 ==> 游覽找到.dll
using JRO;//該命名空間包含壓縮ACCESS的類(方法)
namespace EC
{
/// <summary>
/// 數據庫恢復和備份
/// </summary>
public class SqlBackObject
{
public SqlBackObject()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
#region SQL數據庫備份
/// <summary>
/// SQL數據庫備份
/// </summary>
/// <param name="ServerIP">SQL服務器IP或(Localhost)</param>
/// <param name="LoginName">數據庫登錄名</param>
/// <param name="LoginPass">數據庫登錄密碼</param>
/// <param name="DBName">數據庫名</param>
/// <param name="BackPath">備份到的路徑</param>
public static void SQLBACK(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ServerIP, LoginName, LoginPass);
oBackup.Database = DBName;
oBackup.Files = BackPath;
oBackup.BackupSetName = DBName;
oBackup.BackupSetDescription = "數據庫備份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion
#region SQL恢復數據庫
/// <summary>
/// SQL恢復數據庫
/// </summary>
/// <param name="ServerIP">SQL服務器IP或(Localhost)</param>
/// <param name="LoginName">數據庫登錄名</param>
/// <param name="LoginPass">數據庫登錄密碼</param>
/// <param name="DBName">要還原的數據庫名</param>
/// <param name="BackPath">數據庫備份的路徑</param>
public static void SQLDbRestore(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
{
SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ServerIP, LoginName, LoginPass);
orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
orestore.Database = DBName;
orestore.Files = BackPath;
orestore.FileNumber = 1;
orestore.ReplaceDatabase = true;
orestore.SQLRestore(oSQLServer);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion
#region 根據指定的文件名稱創建Access數據庫
/// <summary>
/// 根據指定的文件名稱創建數據
/// </summary>
/// <param name="DBPath">絕對路徑+文件名稱</param>
public static void CreateAccess(string DBPath)
{
if (File.Exists(DBPath))//檢查數據庫是否已存在
{
throw new Exception("目標數據庫已存在,無法創建");
}
DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
//創建一個CatalogClass對象實例
ADOX.CatalogClass cat = new ADOX.CatalogClass();
//使用CatalogClass對象的Create方法創建ACCESS數據庫
cat.Create(DBPath);
}
#endregion
#region 壓縮Access數據庫
/// <summary>
/// 壓縮Access數據庫
/// </summary>
/// <param name="DBPath">數據庫絕對路徑</param>
public static void CompactAccess(string DBPath)
{
if (!File.Exists(DBPath))
{
throw new Exception("目標數據庫不存在,無法壓縮");
}
//聲明臨時數據庫名稱
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".bak";
temp = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp;
//定義臨時數據庫的連接字符串
string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+temp;
//定義目標數據庫的連接字符串
string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
//創建一個JetEngineClass對象的實例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass對象的CompactDatabase方法壓縮修復數據庫
jt.CompactDatabase(DBPath2, temp2);
//拷貝臨時數據庫到目標數據庫(覆蓋)
File.Copy(temp, DBPath, true);
//最後刪除臨時數據庫
File.Delete(temp);
}
#endregion
#region 備份Access數據庫
/// <summary>
/// 備份Access數據庫
/// </summary>
/// <param name="srcPath">要備份的數據庫絕對路徑</param>
/// <param name="aimPath">備份到的數據庫絕對路徑</param>
/// <returns></returns>
public static void Backup(string srcPath,string aimPath)
{
if (!File.Exists(srcPath))
{
throw new Exception("源數據庫不存在,無法備份");
}
try
{
File.Copy(srcPath,aimPath,true);
}
catch(IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
#endregion
#region 還原Access數據庫
/// <summary>
/// 還原Access數據庫
/// </summary>
/// <param name="bakPath">備份的數據庫絕對路徑</param>
/// <param name="dbPath">要還原的數據庫絕對路徑</param>
public static void RecoverAccess(string bakPath,string dbPath)
{
if (!File.Exists(bakPath))
{
throw new Exception("備份數據庫不存在,無法還原");
}
try
{
File.Copy(bakPath, dbPath, true);
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
#endregion
}
}