asp.net 備份和恢復(fù)數(shù)據(jù)庫的方法示例
/**********************************************************************************
*
* 功能說明:備份和恢復(fù)SQL Server數(shù)據(jù)庫
* 作者: 劉功勛;
* 版本:V0.1(C#2.0);時(shí)間:2007-1-1
* 當(dāng)使用SQL Server時(shí),請(qǐng)引用 COM組件中的,SQLDMO.dll組件
* 當(dāng)使用Access中,請(qǐng)瀏覽添加引用以下兩個(gè)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;//該命名空間包含創(chuàng)建ACCESS的類(方法)--解決方案 ==> 引用 ==> 添加引用 ==> 游覽找到.dll
using JRO;//該命名空間包含壓縮ACCESS的類(方法)
namespace EC
{
/// <summary>
/// 數(shù)據(jù)庫恢復(fù)和備份
/// </summary>
public class SqlBackObject
{
public SqlBackObject()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
#region SQL數(shù)據(jù)庫備份
/// <summary>
/// SQL數(shù)據(jù)庫備份
/// </summary>
/// <param name="ServerIP">SQL服務(wù)器IP或(Localhost)</param>
/// <param name="LoginName">數(shù)據(jù)庫登錄名</param>
/// <param name="LoginPass">數(shù)據(jù)庫登錄密碼</param>
/// <param name="DBName">數(shù)據(jù)庫名</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 = "數(shù)據(jù)庫備份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion
#region SQL恢復(fù)數(shù)據(jù)庫
/// <summary>
/// SQL恢復(fù)數(shù)據(jù)庫
/// </summary>
/// <param name="ServerIP">SQL服務(wù)器IP或(Localhost)</param>
/// <param name="LoginName">數(shù)據(jù)庫登錄名</param>
/// <param name="LoginPass">數(shù)據(jù)庫登錄密碼</param>
/// <param name="DBName">要還原的數(shù)據(jù)庫名</param>
/// <param name="BackPath">數(shù)據(jù)庫備份的路徑</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 根據(jù)指定的文件名稱創(chuàng)建Access數(shù)據(jù)庫
/// <summary>
/// 根據(jù)指定的文件名稱創(chuàng)建數(shù)據(jù)
/// </summary>
/// <param name="DBPath">絕對(duì)路徑+文件名稱</param>
public static void CreateAccess(string DBPath)
{
if (File.Exists(DBPath))//檢查數(shù)據(jù)庫是否已存在
{
throw new Exception("目標(biāo)數(shù)據(jù)庫已存在,無法創(chuàng)建");
}
DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
//創(chuàng)建一個(gè)CatalogClass對(duì)象實(shí)例
ADOX.CatalogClass cat = new ADOX.CatalogClass();
//使用CatalogClass對(duì)象的Create方法創(chuàng)建ACCESS數(shù)據(jù)庫
cat.Create(DBPath);
}
#endregion
#region 壓縮Access數(shù)據(jù)庫
/// <summary>
/// 壓縮Access數(shù)據(jù)庫
/// </summary>
/// <param name="DBPath">數(shù)據(jù)庫絕對(duì)路徑</param>
public static void CompactAccess(string DBPath)
{
if (!File.Exists(DBPath))
{
throw new Exception("目標(biāo)數(shù)據(jù)庫不存在,無法壓縮");
}
//聲明臨時(shí)數(shù)據(jù)庫名稱
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;
//定義臨時(shí)數(shù)據(jù)庫的連接字符串
string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+temp;
//定義目標(biāo)數(shù)據(jù)庫的連接字符串
string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
//創(chuàng)建一個(gè)JetEngineClass對(duì)象的實(shí)例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass對(duì)象的CompactDatabase方法壓縮修復(fù)數(shù)據(jù)庫
jt.CompactDatabase(DBPath2, temp2);
//拷貝臨時(shí)數(shù)據(jù)庫到目標(biāo)數(shù)據(jù)庫(覆蓋)
File.Copy(temp, DBPath, true);
//最后刪除臨時(shí)數(shù)據(jù)庫
File.Delete(temp);
}
#endregion
#region 備份Access數(shù)據(jù)庫
/// <summary>
/// 備份Access數(shù)據(jù)庫
/// </summary>
/// <param name="srcPath">要備份的數(shù)據(jù)庫絕對(duì)路徑</param>
/// <param name="aimPath">備份到的數(shù)據(jù)庫絕對(duì)路徑</param>
/// <returns></returns>
public static void Backup(string srcPath,string aimPath)
{
if (!File.Exists(srcPath))
{
throw new Exception("源數(shù)據(jù)庫不存在,無法備份");
}
try
{
File.Copy(srcPath,aimPath,true);
}
catch(IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
#endregion
#region 還原Access數(shù)據(jù)庫
/// <summary>
/// 還原Access數(shù)據(jù)庫
/// </summary>
/// <param name="bakPath">備份的數(shù)據(jù)庫絕對(duì)路徑</param>
/// <param name="dbPath">要還原的數(shù)據(jù)庫絕對(duì)路徑</param>
public static void RecoverAccess(string bakPath,string dbPath)
{
if (!File.Exists(bakPath))
{
throw new Exception("備份數(shù)據(jù)庫不存在,無法還原");
}
try
{
File.Copy(bakPath, dbPath, true);
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
#endregion
}
}
- 深入分析緩存依賴中cachedependency對(duì)象及周邊小講
- asp.net開發(fā)中怎樣去突破文件依賴緩存
- 開啟SQLSERVER數(shù)據(jù)庫緩存依賴優(yōu)化網(wǎng)站性能
- SQL Server 高速緩存依賴分析
- 使用Memcache緩存mysql數(shù)據(jù)庫操作的原理和緩存過程淺析
- mysql實(shí)現(xiàn)本地keyvalue數(shù)據(jù)庫緩存示例
- asp.net連接數(shù)據(jù)庫讀取數(shù)據(jù)示例分享
- asp.net 通用的連接數(shù)據(jù)庫實(shí)例代碼
- ASP.NET數(shù)據(jù)庫緩存依賴實(shí)例分析
相關(guān)文章
.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)塑形
在查詢的場(chǎng)景中,還有一類需求不是很常見,就是在前端請(qǐng)求中指定返回的字段。所以這篇文章主要介紹了.NET 6如何實(shí)現(xiàn)數(shù)據(jù)塑形,需要的可以參考一下2022-01-01ASP.NET中利用DataList實(shí)現(xiàn)圖片無縫滾動(dòng) 實(shí)例分享
這個(gè)問題之前也困擾我,后來解決了,拿出來分享下,以后用也方便,代碼很容易看懂,不多說什么了2013-06-06Asp.net中的GridView導(dǎo)出遇到的兩個(gè)問題和解決方法
Asp.net下GridView導(dǎo)出遇到的兩個(gè)問題與解決方法,需要的朋友可以參考一下。2009-12-12ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后臺(tái)管理系統(tǒng)之前端頁面框架構(gòu)建源碼分享
這篇文章主要為大家分享了ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后臺(tái)管理系統(tǒng)之easyui前端頁面框架構(gòu)建源碼,感興趣的小伙伴們可以參考一下2016-07-07ASP.NET中上傳并讀取Excel文件數(shù)據(jù)示例
如何打開Excel數(shù)據(jù)庫文件,想必有很多朋友都不清楚吧,下面通過一個(gè)簡(jiǎn)單的例子,實(shí)現(xiàn)讀取Excel數(shù)據(jù)文件2014-05-05使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移
這篇文章介紹了使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09asp.net Grid 導(dǎo)出Excel實(shí)現(xiàn)程序代碼
看了FineUI中的將Grid導(dǎo)出為Excel的實(shí)現(xiàn)方法,實(shí)際上是可以非常簡(jiǎn)單??磥砗茈y的問題,變換一種思路就可以非常簡(jiǎn)單2012-12-12如何在ASP.NET Core應(yīng)用程序運(yùn)行Vue并且部署在IIS上詳解
這篇文章主要給大家介紹了關(guān)于如何運(yùn)行Vue在ASP.NET Core應(yīng)用程序并且部署在IIS上的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10