asp.net methods for backing up and restoring a database _ practical tips

Source: Internet
Author: User
Tags access database

Copy Code code as follows:

/**********************************************************************************

 *
 * feature Description: Back up and restore SQL Server database
 * Author: Liu Gongxun;
 * version: V0.1 (c#2.0); Time: 2007-1-1
 * when using SQL Server, refer to the SQLDMO.dll component in the COM component
 * when using Access, Please browse to add reference to the following two DLLs
 *          reference C:\Program Files\Common Files\ System\ado\msadox.dll, this DLL contains ADOX namespaces
 *          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; The
using adox;//this namespace contains classes (methods) that create access--Solution ==> Reference ==> Add Reference ==> Browse to the. dll
using jro;//the namespace contains classes that compress access (method )

Namespace EC
{
<summary>
Database Recovery and Backup
</summary>
public class Sqlbackobject
{
Public Sqlbackobject ()
{
//
TODO: Add constructor logic here
//
}

#region SQL database backup
<summary>
SQL database Backup
</summary>
<param name= "ServerIP" >sql server IP or (Localhost) </param>
<param name= "LoginName" > Database login </param>
<param name= "Loginpass" > Database login Password </param>
<param name= "dbname" > Database name </param>
<param name= "Backpath" > Backup to Path </param>
public static void Sqlback (String serverip,string loginname,string loginpass,string dbname,string backpath)
{
SQLDMO. Backup obackup = new SQLDMO. Backupclass ();
SQLDMO. SQL Server 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 Recovery Database
<summary>
SQL Recovery Database
</summary>
<param name= "ServerIP" >sql server IP or (Localhost) </param>
<param name= "LoginName" > Database login </param>
<param name= "Loginpass" > Database login Password </param>
<param name= "dbname" > Database name to restore </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. SQL Server 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 Create 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 that the database already exists
{
throw new Exception ("target database already exists, cannot be created");
}
DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +dbpath;
Create a Catalogclass object instance
ADOX. Catalogclass cat = new ADOX. Catalogclass ();
Create an Access database by using the Create method of the Catalogclass object
Cat. Create (DBPath);

}
#endregion

#region Compress an Access database
<summary>
Compress an Access database
</summary>
<param name= "DBPath" > Database absolute path </param>
public static void Compactaccess (String dbpath)
{
if (! File.exists (DBPath))
{
throw new Exception ("destination database does not exist, cannot be compressed");
}

Declaring a 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 a connection string for the staging database
String temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +temp;
Define the connection string for the target database
String DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +dbpath;
Create an instance of a Jetengineclass object
JRO. Jetengineclass JT = new JRO. Jetengineclass ();
To compress a repair database using the CompactDatabase method of the Jetengineclass object
Jt.compactdatabase (DBPath2, TEMP2);
Copy temporary database to target database (overwrite)
File.Copy (temp, dbpath, true);
Finally delete the staging database
File.delete (temp);
}
#endregion

#region back up an Access database
<summary>
Back up an Access database
</summary>
<param name= "Srcpath" > Absolute path of database to be backed up </param>
<param name= "Aimpath" > Backup to Database absolute path </param>
<returns></returns>
public static void Backup (String srcpath,string aimpath)
{

if (! File.exists (Srcpath))
{
throw new Exception ("The source database does not exist, cannot be backed up");
}
Try
{
File.Copy (srcpath,aimpath,true);
}
catch (IOException ixp)
{
throw new Exception (IXP. ToString ());
}

}

#endregion

#region Restore an Access database
<summary>
Restore an Access database
</summary>
<param name= "Bakpath" > Backup Database absolute path </param>
<param name= "DBPath" > Absolute path of database to restore </param>
public static void Recoveraccess (String bakpath,string dbpath)
{
if (! File.exists (Bakpath))
{
throw new Exception ("Backup database does not exist, 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.