Merhaba arkadaşlar bu yazımda

database backupını alıp o güne ait isim verip onu sıkıştıran bir class yazacağım

using System;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;
/// <summary>
/// database_backup
/// </summary>
public class take_backup
{
    public take_backup()
    {
    }
    SqlConnection baglanti;
    private void BaglantiAc()
    {
        baglanti.Open();
    }
    private void BaglantiKapat()
    {
        baglanti.Close();
    }
    string query;
    /// <summary>
    /// database frm name example G_ICT 
    /// path name  example \Alg-sqlg_ict_imsDatabase Backups
    /// </summary>
    /// <param name=”database_firm_name”></param>
    /// <param name=”path”></param>
    //database frm name G_ICT
    //path name \Alg-sqlg_ict_imsDatabase Backups
    public void backup_al(string database_firm_name,string path)
    {
        baglanti = new SqlConnection(@”Data Source=192.168.9.13;Network Library=DBMSSOCN;
Initial Catalog=”+database_firm_name+”_IMS;User ID=Ramazan;Password=T4f254*-;”);
        BaglantiAc();
        //backup alma işlemi yapıyoruz
        query = @”declare @deneme nvarchar(maX)
        declare @date_day nvarchar(10)
        SELECT @date_day=replace(convert(NVARCHAR, getdate(), 105), ‘-‘, ‘_’)
        set @deneme=’
        BACKUP DATABASE [“+database_firm_name+@”_IMS] TO  
        DISK = N”E:”+database_firm_name+@”_IMSDatabase Backups”+database_firm_name+@”_’+@date_day+’.bak”
        WITH NOFORMAT, INIT,  NAME = N””+database_firm_name+@”_IMS-Full Database Backup”,
         SKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10
         ‘
         DECLARE @db SYSNAME = N'”+database_firm_name+@”_IMS’;
        DECLARE @data NVARCHAR(MAX) = QUOTENAME(@db) + ‘.sys.sp_executesql’;
        exec @data @deneme
                “;
        SqlCommand cmd = new SqlCommand(query, baglanti);
        //5000 saniye olarak ayarlıyoruz
        cmd.CommandTimeout = 5000;
        cmd.ExecuteNonQuery();
        BaglantiKapat();
        string database_tarihi = DateTime.Now.ToString(“dd_MM_yyyy”);
        //Sıkıştırma işlemi
        FileInfo file = new FileInfo(path+database_firm_name+”_” + database_tarihi + “.bak”);
        Compress(file);
    }
    public static void Compress(FileInfo fi)
    {
        // Get the stream of the source file.
        using (FileStream inFile = fi.OpenRead())
        {
            // Prevent compressing hidden and already compressed files.
            if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
                    != FileAttributes.Hidden & fi.Extension != “.gz”)
            {
                // Create the compressed file.
                using (FileStream outFile = File.Create(fi.FullName + “.gz”))
                {
                    using (GZipStream Compress = new GZipStream(outFile,
                            CompressionMode.Compress))
                    {
                        // Copy the source file into the compression stream.
                        byte[] buffer = new byte[4096];
                        int numRead;
                        while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            Compress.Write(buffer, 0, numRead);
                        }
                    }
                }
            }
        }
    }
}
Ramazan CESUR