* 功能說明:備份和恢複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 SQLDatabase Backup
/// <summary>
/// SQLDatabase Backup
/// </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 = "Database Backup";
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">Database Backup的路徑</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
}
}
===============================================================================================
請添加引用Microsoft ADO Ext. 2.7 for DDL and Securit...
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 ADOX;
/// <summary>
/// file : create access db
/// autor: Wang Yahui
/// createtime : 2008-03-17
/// </summary>
public class CreateDB
{
/// <summary>
/// create access db
/// </summary>
/// <param name="Path">db file path</param>
public CreateDB(string Path)
{
//為了方便測試,資料庫名字採用比較隨機的名字,以防止添加不成功時還需要重新啟動IIS來刪除資料庫。
string dbName = Path +DateTime.Now.Millisecond.ToString()+".mdb";
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";");
///建立StudentPaper表
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = cat;
tbl.Name = Constant.StudentPaper;
///添加StudentID欄位
ADOX.ColumnClass studentID = new ADOX.ColumnClass();
studentID.ParentCatalog = cat;
studentID.Type = ADOX.DataTypeEnum.adWChar; // 必須先設定欄位類型
studentID.Name = "StudentID";
studentID.Properties["Jet OLEDB:Allow Zero Length"].Value = false;//是否可以為空白
//col.Properties["AutoIncrement"].Value= true;//增加一個自動成長的欄位
tbl.Columns.Append(studentID, ADOX.DataTypeEnum.adWChar, 20);
///增加PaperType欄位
ADOX.ColumnClass paperType = new ADOX.ColumnClass();
paperType.ParentCatalog = cat;
paperType.Type = ADOX.DataTypeEnum.adWChar;
paperType.Name = "PaperType";
paperType.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(paperType, ADOX.DataTypeEnum.adWChar, 1);
//增加oMRAnswer欄位
ADOX.ColumnClass oMRAnswer = new ADOX.ColumnClass();
oMRAnswer.ParentCatalog = cat;
oMRAnswer.Type = ADOX.DataTypeEnum.adWChar;
oMRAnswer.Name = "OMRAnswer";
oMRAnswer.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(oMRAnswer, ADOX.DataTypeEnum.adWChar, 255);
//增加oMRAnswer欄位
ADOX.ColumnClass imageStudentID = new ADOX.ColumnClass();
imageStudentID.ParentCatalog = cat;
imageStudentID.Type = ADOX.DataTypeEnum.adWChar;
imageStudentID.Name = "ImageStudentID";
imageStudentID.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(imageStudentID, ADOX.DataTypeEnum.adWChar, 6);
///設定主鍵
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "StudentID", "", "");
cat.Tables.Append(tbl);
tbl = null;
cat = null;
} // end function CreateDB()
}
如果你建立資料庫表時報“類型無效”的錯誤,請參考下表,對照一下欄位的資料類型是否一致:
常數 |
值 |
Jet 3.51 |
Jet 4.0 |
SQL 7.0 |
adBinary |
128 |
yes |
yes |
yes |
adBoolean |
11 |
yes |
yes |
yes |
adChar |
129 |
yes |
否 |
yes |
adCurrency |
6 |
yes |
yes |
yes |
adDate |
7 |
yes |
yes |
否 |
adDouble |
5 |
yes |
yes |
yes |
adGUID |
72 |
yes |
yes |
yes |
adInteger |
3 |
yes |
yes |
yes |
adLongVarBinary |
205 |
yes |
yes |
yes |
adLongVarChar |
201 |
yes |
否 |
yes |
adLongVarWChar |
203 |
否 |
yes |
yes |
adNumeric |
131 |
否 |
是的 (帶有資訊) * |
是的 (帶有資訊) * |
adSingle |
4 |
yes |
yes |
yes |
adSmallInt |
2 |
yes |
yes |
yes |
adUnsignedTinyInt |
17 |
yes |
yes |
yes |
adVarBinary |
204 |
yes |
yes |
yes |
adVarChar |
200 |
yes |
否 |
yes |
adVarWChar |
202 |
否 |
yes |
yes |
adWChar |
130 |
否 |
yes |
yes |
adDBTimeStamp |
135 |
否 |
否 |
yes |