asp.net database backup Restore (sqlserver+access) _ Practical Tips
Last Update:2017-01-18
Source: Internet
Author: User
/**********************************************************************************
*
* Function Description: Backup and restore SQL Server database
* Author: Liu Gongxun;
* Version: V0.1 (c#2.0); Time: 2007-1-1
* Refer to the SQLDMO.dll component in the COM component when using SQL Server
* When using Access, browse to add references to the following two DLLs
* Reference C:\Program Files\Common Files\system\ado\msadox.dll, which contains ADOX namespaces
* Reference C:\Program Files\Common Files\system\ado\msjro.dll, which contains JRO namespaces
* *******************************************************************************/
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 classes (methods) that create access--solutions ==> references ==> Add references ==> tours find. dll
Using jro;//This namespace contains classes that compress access (methods)
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
}
}