Winform program, back up database + and compress + and delete previous backups

Source: Internet
Author: User

Note: To regularly back up the database on the server and compress it to the specified directory, you can easily download it locally and write this program. With the windows task plan, you can back up the database regularly.

The Program must reference SQLDMO. DLL. If sqlserver is installed on the computer, you can find it in C: \ Program Files \ Microsoft SQL Server \ 80 \ Tools \ Binn \

 

String ServerName = System. Configuration. ConfigurationSettings. AppSettings ["ServerName"]; // database server name
String UserName = System. Configuration. ConfigurationSettings. deleettings ["UserName"]; // database UserName
String Password = System. Configuration. ConfigurationSettings. receivettings ["Password"]; // Password
String strDbName = System. Configuration. ConfigurationSettings. deleettings ["strDbName"]; // Database Name
String FilePathBak = System. Configuration. ConfigurationSettings. deleettings ["FilePathBak"]; // path of the backup file (. bak), excluding the file name
String FilePathRar = System. Configuration. ConfigurationSettings. resumettings ["FilePathRar"]; // path to save the compressed file, which does not include the compressed file name

Public Form1 ()
{
InitializeComponent ();

Bool backup = BackUPDB (ServerName, UserName, Password, strDbName, FilePathBak );
If (backup)
{
Rar (FilePathBak, FilePathRar );
Del ();
}
System. Environment. Exit (0); // This is the most thorough Exit method. No matter what thread is forced to Exit, the program ends very cleanly.
}

/// <Summary>
/// Back up the database
/// </Summary>
/// <Param name = "ServerName"> server name </param>
/// <Param name = "UserName"> Logon account </param>
/// <Param name = "Password"> Password </param>
/// <Param name = "strDbName"> name of the database to be backed up </param>
/// <Param name = "FilePathBak"> path of the backup file </param>
/// <Returns> </returns>
Public bool BackUPDB (string ServerName, string UserName, string Password, string strDbName, string FilePathBak)
{
SQLDMO. svr = new SQLDMO. SQLServerClass ();
SQLDMO. Backup bak = new SQLDMO. BackupClass ();
Try
{
Svr. LoginSecure = false;
Svr. Connect (ServerName, UserName, Password );
Bak. Action = 0; // SQLDMO. SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
Bak. Initialize = true;

Bak. files = FilePathBak + strDbName + System. dateTime. now. toString ("yyyyMMdd") + ". bak "; // path of the backup file + file name (F: \ databack \ fireweb20100810.bak)
Bak. Database = strDbName;
Bak. BackupSetName = strDbName;
Bak. BackupSetDescription = "database backup ";
Bak. SQLBackup (svr );

Return true;
}
Catch (Exception err)
{
Throw (new Exception ("failed to back up the database" + err. Message ));
}
Finally
{
Svr. DisConnect ();
}
}

/// <Summary>
/// Compress the specified file to the specified folder
/// </Summary>
/// <Param name = "path"> path of the compressed file, which must be specified in the method because the file name must be determined by the current time </param>
/// <Param name = "rarPath"> Compressed Storage path </param>
/// <Returns> </returns>
Public bool rar (string path, string rarPath)
{
Bool flag = false;
System. Diagnostics. Process Pc1 = new System. Diagnostics. Process ();
Pc1.StartInfo. FileName = "Winrar.exe ";
Pc1.StartInfo. CreateNoWindow = false;
String fileName = strDbName + System. DateTime. Now. ToString ("yyyyMMdd") + ". bak ";
String rarFileName = strDbName + System. DateTime. Now. ToString ("yyyyMMdd") + ". rar ";
Path + = fileName; // the file path + file name before decompression
RarPath + = rarFileName; // decompressed path + file name
Pc1.StartInfo. Arguments = "a-ep" + rarPath + "" + path; //-ep indicates that the path is excluded from the name, that is, directories at different layers are not included during compression.
Pc1.Start ();
If (Pc1.HasExited)
{
Int iExitCode = Pc1.ExitCode;
If (iExitCode = 0)
{
Flag = true;
}
Else
{
Flag = false;
}
Pc1.Kill ();
Pc1.Close ();
Pc1.Dispose ();
}
Return flag;
}

/// <Summary>
/// Because the backup is performed once a day, the redundant backup files and compressed files must be deleted. Only the latest five backup files must be retained.
/// </Summary>
Public void del ()
{
// Delete the bak File
Int count1 = 0;
ArrayList al1 = new ArrayList ();
DirectoryInfo diPathBak = new DirectoryInfo (FilePathBak );
Foreach (FileInfo fi in diPathBak. GetFiles ())
{
If (fi. Name. Length> 12) // the file Name Length of each backup bak must be greater than 12 (20100808.bak)
{
If (fi. name. substring (0, fi. name. length-fi. extension. length-8 ). equals (strDbName) & fi. extension. equals (". bak "))
{
Count1 ++;
Al1.Add (fi. Name );
If (count1> 5)
{
String filename = al1 [0]. ToString (); // Delete the first object (because the GetFiles () method obtains the object by name in ascending order, that is, deleting the earliest object)
File. Delete (diPathBak + filename );
Al1.RemoveAt (0 );
Count1 --;
}
}
}
}

// Delete the rarfile
Int count = 0;
ArrayList al = new ArrayList ();
DirectoryInfo diPathRar = new DirectoryInfo (FilePathRar );
Foreach (FileInfo fi in diPathRar. GetFiles ())
{
If (fi. Name. Length> 12) // the file Name Length of each backup bak must be greater than 12 (20100808.bak)
{
If (fi. name. substring (0, fi. name. length-fi. extension. length-8 ). equals (strDbName) & fi. extension. equals (". rar "))
{
Count ++;
Al. Add (fi. Name );
If (count> 5)
{
String filename = al [0]. ToString (); // Delete the first file (because the GetFiles () method sorts the file by name in ascending order, that is, delete the earliest)
File. Delete (diPathRar + filename );
Al. RemoveAt (0 );
Count --;
}

}
}
}
}

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.