Automatically backs up the SQL database and deletes the backup file before the specified date

Source: Internet
Author: User

<summary>
Data backup
</summary>
///

public bool DataBackup (string getpath)
{
Try
{
SqlConnection con = new SqlConnection (); Using code to connect to a database

Con. ConnectionString = configurationsettings.appsettings["DataBase"]. ToString ();
Con. Open ();
SqlCommand com = new SqlCommand ();
Com.commandtext = "BACKUP DATABASE Name to DISK = '" + GetPath + "'";
Com.       Connection = con; Connection
Com. ExecuteNonQuery ();

int i = com. ExecuteNonQuery ();
if (COM. ExecuteNonQuery () ==-1)
//{
Showexclamation ("Data has been successfully backed up! ");
//}
Con. Close ();
Con. Dispose ();
return true;
}
catch (Exception k)
{
MessageBox.Show (K.message);
return false;
}
}

if (! System.IO.Directory.Exists ("d:\\ backup File"))
{
System.IO.Directory.CreateDirectory ("d:\\ backup file");
}
string path = @ "d:\ backup file \" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". Bak";
This.dqlib.DataBackup (path);
DirectoryInfo BaseDir = new DirectoryInfo ("d:\\ backup file");
Gets all the files under the specified folder.
If you need to get files in a particular format, such as. html, you can write Basedir.getfiles ("*.html");
fileinfo[] files = basedir.getfiles ();

Define a file name string
String fileNames = String. Empty;

if (Files. Length > 10)
{
for (int i = 0; i < files. Length; i++)
{
Get each file name and log it to the string fileNames
If you need to get the full path name of the file, Files[i]. FullName;
if (Datetime.compare (files[i). CreationTime, DateTime.Now.AddDays ( -10)) < 0)
{
Files[i]. Delete ();
}

}
}

Automatically backs up the SQL database and deletes the backup file before the specified date

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.