這篇文章給大家介紹asp.net 中怎么備份和恢復(fù)數(shù)據(jù)庫,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)主營競秀網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,競秀h5微信小程序定制開發(fā)搭建,競秀網(wǎng)站營銷推廣歡迎競秀等地區(qū)企業(yè)咨詢代碼如下:
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
{
///
/// 數(shù)據(jù)庫恢復(fù)和備份
///
public class SqlBackObject
{
public SqlBackObject()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
#region SQL數(shù)據(jù)庫備份
///
/// SQL數(shù)據(jù)庫備份
///
/// SQL服務(wù)器IP或(Localhost)
/// 數(shù)據(jù)庫登錄名
/// 數(shù)據(jù)庫登錄密碼
/// 數(shù)據(jù)庫名
/// 備份到的路徑
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ù)庫
///
/// SQL恢復(fù)數(shù)據(jù)庫
///
/// SQL服務(wù)器IP或(Localhost)
/// 數(shù)據(jù)庫登錄名
/// 數(shù)據(jù)庫登錄密碼
/// 要還原的數(shù)據(jù)庫名
/// 數(shù)據(jù)庫備份的路徑
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ù)庫
///
/// 根據(jù)指定的文件名稱創(chuàng)建數(shù)據(jù)
///
/// 絕對路徑+文件名稱
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)建一個CatalogClass對象實(shí)例
ADOX.CatalogClass cat = new ADOX.CatalogClass();
//使用CatalogClass對象的Create方法創(chuàng)建ACCESS數(shù)據(jù)庫
cat.Create(DBPath);
}
#endregion
#region 壓縮Access數(shù)據(jù)庫
///
/// 壓縮Access數(shù)據(jù)庫
///
/// 數(shù)據(jù)庫絕對路徑
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 DBPath3 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
//創(chuàng)建一個JetEngineClass對象的實(shí)例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass對象的CompactDatabase方法壓縮修復(fù)數(shù)據(jù)庫
jt.CompactDatabase(DBPath3, temp2);
//拷貝臨時(shí)數(shù)據(jù)庫到目標(biāo)數(shù)據(jù)庫(覆蓋)
File.Copy(temp, DBPath, true);
//最后刪除臨時(shí)數(shù)據(jù)庫
File.Delete(temp);
}
#endregion
#region 備份Access數(shù)據(jù)庫
///
/// 備份Access數(shù)據(jù)庫
///
/// 要備份的數(shù)據(jù)庫絕對路徑
/// 備份到的數(shù)據(jù)庫絕對路徑
///
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ù)庫
///
/// 還原Access數(shù)據(jù)庫
///
/// 備份的數(shù)據(jù)庫絕對路徑
/// 要還原的數(shù)據(jù)庫絕對路徑
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
}
}
關(guān)于asp.net 中怎么備份和恢復(fù)數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。