/*************************************** **************************************** ***
*
* Function Description: backs up and recovers SQL Server databases.
* Author: Liu gongxun;
* Version: V0.1 (C #2.0); time:
* When using SQL Server, reference the SQLDMO. dll component in the COM component.
* When using Access, browse to add reference to the following two dll
* Reference C: \ Program Files \ Common Files \ System \ ado \ msadox. dll, which contains the ADOX namespace
* Reference C: \ Program Files \ Common Files \ System \ ado \ msjro. dll, which contains the JRO namespace
**************************************** ****************************************/
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; // The namespace contains the class (method) for creating ACCESS -- Solution ==> reference ==> add reference ==> find. dll
Using JRO; // The namespace contains the class (method) for compressing ACCESS)
Namespace EC
{
/// <Summary>
/// Database recovery and backup
/// </Summary>
Public class SqlBackObject
{
Public SqlBackObject ()
{
//
// TODO: add the constructor logic here
//
}
# Region SQL database backup
/// <Summary>
/// SQL database backup
/// </Summary>
/// <Param> SQL Server IP address or (Localhost) </param>
/// <Param> database login name </param>
/// <Param> database logon password </param>
/// <Param> database name </param>
/// <Param> backup path </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 Restore database
/// <Summary>
/// SQL Restore database
/// </Summary>
/// <Param> SQL Server IP address or (Localhost) </param>
/// <Param> database login name </param>
/// <Param> database logon password </param>
/// <Param> name of the database to be restored </param>
/// <Param> path of 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 creates an Access database based on the specified file name
/// <Summary>
/// Create data based on the specified file name
/// </Summary>
/// <Param> absolute path + file name </param>
Public static void CreateAccess (string DBPath)
{
If (File. Exists (DBPath) // check whether the database already Exists
{
Throw new Exception ("the target database already exists and cannot be created ");
}
DBPath = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + DBPath;
// Create a CatalogClass object instance
ADOX. CatalogClass cat = new ADOX. CatalogClass ();
// Use the Create method of the CatalogClass object to Create an ACCESS database
Cat. Create (DBPath );
}
# Endregion
# Region compressed Access Database
/// <Summary>
/// Compress the Access database
/// </Summary>
/// <Param> absolute database path </param>
Public static void CompactAccess (string DBPath)
{
If (! File. Exists (DBPath ))
{
Throw new Exception ("the target database does not exist and cannot be compressed ");
}
// Declare the temporary database name
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;
// Define the connection string of the temporary database
String temp2 = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + temp;
// Define the connection string of the target database
String DBPath2 = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + DBPath;
// Create an instance of the JetEngineClass object
JRO. JetEngineClass jt = new JRO. JetEngineClass ();
// Use the CompactDatabase method of the JetEngineClass object to compress and restore the database
Jt. CompactDatabase (DBPath2, temp2 );
// Copy the temporary database to the target database (overwrite)
File. Copy (temp, DBPath, true );
// Delete the temporary database
File. Delete (temp );
}
# Endregion
# Region backup Access Database
/// <Summary>
/// Back up the Access database
/// </Summary>
/// <Param> absolute path of the database to be backed up </param>
/// <Param> absolute path of the database to be backed up </param>
/// <Returns> </returns>
Public static void Backup (string srcPath, string aimPath)
{
If (! File. Exists (srcPath ))
{
Throw new Exception ("the source database does not exist and cannot be backed up ");
}
Try
{
File. Copy (srcPath, aimPath, true );
}
Catch (IOException ixp)
{
Throw new Exception (ixp. ToString ());
}
}
# Endregion
# Region restore Access database
/// <Summary>
/// Restore the Access database
/// </Summary>
/// <Param> absolute path of the backup database </param>
/// <Param> absolute path of the database to be restored </param>
Public static void RecoverAccess (string bakPath, string dbPath)
{
If (! File. Exists (bakPath ))
{
Throw new Exception ("the backup database does not exist and cannot be restored ");
}
Try
{
File. Copy (bakPath, dbPath, true );
}
Catch (IOException ixp)
{
Throw new Exception (ixp. ToString ());
}
}
# Endregion
}
}