Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ADO.NET & Database » SQL Server Backup file in standard Zip format

SQL Server Backup file in standard Zip format

This is SQL Server Backup and restore tool, the system will store the backup files in standard Zip format ,the user-friendly screen let you backup and restore SQL Server database to local harddisk or remote network driver easily and quickly.The program can restore database easily.

Page Views : 32182
Downloads : 1634
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Back_InZipFormat.zip
 
 
Nevron Chart
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

This is SQL Server Backup and restore tool, the system will store the backup files in standard Zip format, the user-friendly screen let you backup and restore SQL Server database to local harddisk or remote network driver easily and quickly.The program can restore database easily.

To do:

  1. First Enter Your Sql Server username and password on corresponding Text Box.
  2. Click on Backup Button.
  3. It will take backup on folder ("application path"\Backup). if folder not found then program will create folder automatically  in application path.
  4. After finish the job then check bkpfile on folder "Application path \Backup" bkp file created or not.

Important Functions

Add reference to SQL-DMO dll

You can do this by right clicking the project in Solution Explorer, then selecting 'Add Reference', COM components and the latest version of "Microsoft SQLDMO Object Library".

Available Server

public void dIsplayServerList(ComboBox cboListName)

{

    try

    {

        SQLDMO.Application oSQLServerDMOApp = new SQLDMO.Application();

        Info.informationLayer info = new Info.informationLayer();

                

        SQLDMO.NameList oNameList;

        oNameList = oSQLServerDMOApp.ListAvailableSQLServers();

        for (int intIndex = 0; intIndex <= oNameList.Count - 1; intIndex++)

        {

            if (oNameList.Item(intIndex as object) != null)

            {

                cboListName.Items.Add(oNameList.Item(intIndex).ToString());

            }

        }

        if (cboListName.Items.Count > 0) cboListName.SelectedIndex = 0;

        else cboListName.Text = "(Local)";

    }

    catch

    {

    }

}

 

Available databases 

 

public void dIsplayDatabases(ComboBox cboDatabase,Info.informationLayer info)

{

    try

    {

        SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();

 

        cboDatabase.Items.Clear();

        SQLServer.Connect(info.strServerName,info.strLoginName,info.strPwd);

        foreach (SQLDMO.Database db in SQLServer.Databases)

        {

            if (db.Name != null)

                cboDatabase.Items.Add(db.Name);

        }

        cboDatabase.Sorted = true;

        if (cboDatabase.Items.Count == 0)cboDatabase.Text = "<No databases found>";

    }

    catch (Exception err)

    {

        info.ErrorMessageDataLayer = err.Message;

    }

}

 

Create backup in zip format : 

 

public void cReateSQLDatabaseBackup(InfoSQLDMO.informationLayer InSQLDMO)

{//This is for Creating Database Backup.

     try

     {

         DateTime dt = DateTime.Now;

         String[] format = { "dd-MM-yy" };

         string date;

         date = dt.ToString(format[0], DateTimeFormatInfo.InvariantInfo);

         string FileName = "bkp" + InSQLDMO.strdbName + date + ".bkp";

         string myDocPath = InSQLDMO.strmyDocPath;

         FileName = myDocPath + "\\Backup\\" + FileName;

         //  this.txtFile.Text = FileName;

         if (Directory.Exists(myDocPath + "\\Backup"))

         {

             if (File.Exists(FileName) == false)

             {

                 SQLDMO._SQLServer SqlSever = new SQLDMO.SQLServerClass();

                 SqlSever.Connect(InSQLDMO.strServerName, InSQLDMO.strLoginName, InSQLDMO.strPwd);

                 SQLDMO.Backup bak = new SQLDMO.BackupClass();

                 bak.Devices = bak.Files;

                 bak.Files = FileName;

                 bak.Database = InSQLDMO.strdbName;

                 bak.SQLBackup(SqlSever);

                 string DestFile = myDocPath + "\\Backup" + "\\" + "bkp" + InSQLDMO.strdbName+ date + ".zip";

                 zIpDatabseFile(FileName, DestFile);

                 if (File.Exists(FileName))

                 {

                     File.Delete(FileName);

                 }

                 InSQLDMO.ErrorMessageDataLayer = "Database Backup Process " + InSQLDMO.strdbName + " Sucessfully Completed"; 

             }

             else

             {

                 InSQLDMO.ErrorMessageDataLayer = "The Backup file " + InSQLDMO.strdbName + " is already  exists !. Do You Want to Continue..... '"; 

             }

         }

         else

         {

             System.IO.Directory.CreateDirectory(myDocPath + "\\Backup");

             SQLDMO._SQLServer SqlSever = new SQLDMO.SQLServerClass();

             SqlSever.Connect(InSQLDMO.strServerName, InSQLDMO.strLoginName, InSQLDMO.strPwd);

             SQLDMO.Backup bak = new SQLDMO.BackupClass();

             bak.Devices = bak.Files;

             bak.Files = FileName;

             bak.Database = InSQLDMO.strdbName;

             bak.SQLBackup(SqlSever);

             string DestFile = myDocPath + "\\Backup" + "\\" + "bkp" + date + ".zip";

             zIpDatabseFile(FileName, DestFile);

             if (File.Exists(FileName))

             {

                 File.Delete(FileName);

             }

             InSQLDMO.ErrorMessageDataLayer = "Backup Process " + InSQLDMO.strdbName + " Sucessfully Completed";

         }

     } 

     catch (Exception err)

     {

         InSQLDMO.ErrorMessageLogicLayer = err.Message;

     }

}

 

Restore database in zip format: 

 

public void cReateSQLDatabaseBackup(InfoSQLDMO.informationLayer InSQLDMO)

{//This is for Creating Database Backup.

    try

    {

        DateTime dt = DateTime.Now;

        String[] format = { "dd-MM-yy" };

        string date;

        date = dt.ToString(format[0], DateTimeFormatInfo.InvariantInfo);

        string FileName = "bkp" + InSQLDMO.strdbName + date + ".bkp";

        string myDocPath = InSQLDMO.strmyDocPath;

        FileName = myDocPath + "\\Backup\\" + FileName;

        //  this.txtFile.Text = FileName;

        if (Directory.Exists(myDocPath + "\\Backup"))

        {

            if (File.Exists(FileName) == false)

            {

                SQLDMO._SQLServer SqlSever = new SQLDMO.SQLServerClass();

                SqlSever.Connect(InSQLDMO.strServerName, InSQLDMO.strLoginName, InSQLDMO.strPwd);

                SQLDMO.Backup bak = new SQLDMO.BackupClass();

                bak.Devices = bak.Files;

                bak.Files = FileName;

                bak.Database = InSQLDMO.strdbName;

                bak.SQLBackup(SqlSever);

                string DestFile = myDocPath + "\\Backup" + "\\" + "bkp" + InSQLDMO.strdbName+ date + ".zip";

                zIpDatabseFile(FileName, DestFile);

                if (File.Exists(FileName))

                {

                    File.Delete(FileName);

                }

                InSQLDMO.ErrorMessageDataLayer = "Database Backup Process " + InSQLDMO.strdbName + " Sucessfully Completed"; 

            }

            else

            {

                InSQLDMO.ErrorMessageDataLayer = "The Backup file " + InSQLDMO.strdbName + " is already  exists !. Do You Want to Continue..... '"; 

            }

        }

        else

        {

            System.IO.Directory.CreateDirectory(myDocPath + "\\Backup");

            SQLDMO._SQLServer SqlSever = new SQLDMO.SQLServerClass();

            SqlSever.Connect(InSQLDMO.strServerName, InSQLDMO.strLoginName, InSQLDMO.strPwd);

            SQLDMO.Backup bak = new SQLDMO.BackupClass();

            bak.Devices = bak.Files;

            bak.Files = FileName;

            bak.Database = InSQLDMO.strdbName;

            bak.SQLBackup(SqlSever);

            string DestFile = myDocPath + "\\Backup" + "\\" + "bkp" + date + ".zip";

            zIpDatabseFile(FileName, DestFile);

            if (File.Exists(FileName))

            {

                File.Delete(FileName);

            }

            InSQLDMO.ErrorMessageDataLayer = "Backup Process " + InSQLDMO.strdbName + " Sucessfully Completed";

        }

    }

    catch (Exception err)

    {

        InSQLDMO.ErrorMessageLogicLayer = err.Message;

    }

}

 

Create file a zip : 

 

private void zIpDatabseFile(string srcPath, string destPath)

{//This is for  Zip a File

    byte[] bufferWrite;

    FileStream fsSource;

    FileStream fsDest;

    GZipStream gzCompressed;

    fsSource = new FileStream(srcPath, FileMode.Open, FileAccess.Read, FileShare.Read);

    bufferWrite = new byte[fsSource.Length];

    fsSource.Read(bufferWrite, 0, bufferWrite.Length);

    fsDest = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write);

    gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true);

    gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);

    fsSource.Close();

    gzCompressed.Close();

    fsDest.Close();

}

 

Create  unzip  a  file :

 

private void uNzIpDatabaseFile(string SrcPath, string DestPath)

{// This is for unzip a files.

    byte[] bufferWrite;

    FileStream fsSource;

    FileStream fsDest;

    GZipStream gzDecompressed;

    fsSource = new FileStream(SrcPath, FileMode.Open, FileAccess.Read, FileShare.Read);

    gzDecompressed = new GZipStream(fsSource, CompressionMode.Decompress, true);

 

    bufferWrite = new byte[4];

    fsSource.Position = (int)fsSource.Length - 4;

    fsSource.Read(bufferWrite, 0, 4);

    fsSource.Position = 0;

    int bufferLength = BitConverter.ToInt32(bufferWrite, 0);

 

    byte[] buffer = new byte[bufferLength + 100];

    int readOffset = 0;

    int totalBytes = 0;

 

    while (true)

    {

        int bytesRead = gzDecompressed.Read(buffer, readOffset, 100);

 

        if (bytesRead == 0)

            break;

 

        readOffset += bytesRead;

        totalBytes += bytesRead;

    }

 

    fsDest = new FileStream(DestPath, FileMode.Create);

    fsDest.Write(buffer, 0, totalBytes);

 

    fsSource.Close();

    gzDecompressed.Close();

    fsDest.Close();

}

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Joshy George
Joshy George , has over 5 years in Microsoft technologies. Certified Microsoft Professional with Master Degree in Computers. Working with Infogenie ITAS Pvt.Ltd,Infopark,Kochin,India, as a Sr.Software Engineer and have good experience in C#,ASP.net ,VB, ASP, SQL Server, smart card application,Tapii,Com ctrls.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
no source code in zip file! by joseph lawrence On February 5, 2007
please be informed what i have downloaded doesnt contain the source code written like in this page. Thanks
Reply | Email | Modify 
Database Backup by jeyanthi On January 5, 2008
How can i use this to restore database form server to local
Reply | Email | Modify 
Thanks but it is not work fo9r backup.......... by ankur On March 23, 2009
your code for restore is not desired as that required. Please reconsider that code. and give solution
Reply | Email | Modify 
uisng it in vb.net code asp net web application by S On January 16, 2010
i tried converting it to vb.net but culd not convert it successfully especially the class files
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.