惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 最新话题
Help Net Security
Help Net Security
N
News | PayPal Newsroom
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
S
Security @ Cisco Blogs
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tor Project blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
S
SegmentFault 最新的问题
J
Java Code Geeks
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 【当耐特】
博客园_首页
H
Hacker News: Front Page
T
Threatpost
Jina AI
Jina AI
博客园 - Franky
月光博客
月光博客
L
LINUX DO - 热门话题
The Cloudflare Blog
H
Heimdal Security Blog
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
Cloudbric
Cloudbric
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
S
Secure Thoughts
T
Tenable Blog
I
Intezer
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 恭喜发财

android 电话状态的监听(来电和去电) (转)Xml DML - 恭喜发财 - 博客园 (转)sql:variable() binding and modify method of xquery: insert XML DML 在WinForm中控制GIF动画的启停的一种方法(转) Working with binary large objects (BLOBs) c# 得到局域网中可用SqlServer服务器列表(转) C#实现UDP协议(转) 健康大讲堂—凡膳皆药 寓医于食 ReportView(RDSL)参考资料 How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6) 汇总c#.net常用函数和方法集 在多线程中如何调用Winform 使用C#在进度条中显示复制文件的进度(转) c#将大文件读取或写入到数据库(带进度条的源码)(转) 写C#自定义控件的心得 Finalize(析构函数)、Dispose、Close 的区别与使用 如何在WinForm中对DataGrid进行分页显示(转) 如何用C#做一个类似于桌面插件的程序(转) c#连接各类数据库大全
SQL Server Backup file in standard Zip format
恭喜发财 · 2008-08-19 · via 博客园 - 恭喜发财

 Back_InZipFormat.zip

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();

}