/************************************ **************************************** ******
*
* Function Description: backs up and recovers SQL Server databases
* Author: Liu gongxun;
* version: v0.1 (C #2.0 ); time: 2007-1-1
* when using SQL Server, reference sqldmo in the COM component. DLL components
* when using access, browse and add references to the following two DLL
* Reference c: \ Program Files \ common files \ System \ ADO \ msadox. DLL, which contains the ADOX namespace
* References c: \ Program Files \ common files \ System \ ADO \ msjro. DLL, this dll 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; // This namespace contains the class (method) for creating access -- Solution ==> reference ==> add reference ==> find the tour. 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 );
}< br> catch (exception e)
{< br> throw new exception (E. tostring ();
}< br> finally
{< br> osqlserver. disconnect ();
}< BR ># 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
}
}
this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/yefengzhixia/archive/2009/05/22/4208979.aspx