Asp.net database backup and restoration (sqlserver + access)

Source: Internet
Author: User

/*************************************** **************************************** ***
*
* 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 name = "ServerIP"> SQL Server IP address or (Localhost) </param>
/// <Param name = "LoginName"> database logon name </param>
/// <Param name = "LoginPass"> database logon password </param>
/// <Param name = "DBName"> database name </param>
/// <Param name = "BackPath"> 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 name = "ServerIP"> SQL Server IP address or (Localhost) </param>
/// <Param name = "LoginName"> database logon name </param>
/// <Param name = "LoginPass"> database logon password </param>
/// <Param name = "DBName"> name of the database to be restored </param>
/// <Param name = "BackPath"> database backup path </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 name = "DBPath"> 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 name = "DBPath"> 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 name = "srcPath"> absolute path of the database to be backed up </param>
/// <Param name = "aimPath"> absolute path of the backup database </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 name = "bakPath"> absolute path of the backup database </param>
/// <Param name = "dbPath"> 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
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.