DataBase dumper on Classic ASP.NET with serialize DB to .CSV and using ICSharpCode.SharpZipLib
In this page I describe my old Database dumper on Classic ASP.NET, I include it to my new project programmer.expert.
This dumper contains 5 files. First three files is a Classic ASP NET form. This is frontend of my DbDumper:
1. Frontend Backup.aspx
1: <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Admin/M1.Master" CodeBehind="~/Admin/Backup.aspx.vb" Inherits="Programmer_expert_www_1.Backup1" %>
2: <%@ Register Src="~/Admin/BackupTableItem.ascx" TagPrefix="uc1" TagName="BackupTableItem" %>
3:
4: <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
5: </asp:Content>
6: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
7:
8: <asp:ImageButton PostBackUrl="~/admin/Backup.aspx" ToolTip="Delete old backup" ID="DelImageButton" runat="server" ImageUrl="~/images/del.png" OnClick="DelImageButton_Click" />
9: <asp:LinkButton Enabled="True" ID="DelLinkButton" runat="server" OnClick="DelLinkButton_Click">Delete old backup</asp:LinkButton>
10: <br />
11: <br />
12: <asp:ImageButton ID="ImageButtonBackupConfig" OnClick="ImageButtonBackupConfig_Click" ToolTip="Create Web.Config backup" runat="server" ImageUrl="~/images/plus.png" CommandName="Web.Config" />
13: <asp:ImageButton ID="ImageButtonDownloadConfig" ToolTip="Download Web.Config" PostBackUrl="~/GetZip.ashx?File=Web.Config" runat="server" ImageUrl="~/images/Down-gray.gif" Enabled="false" />
14: <asp:LinkButton ID="LinkButtonDownloadConfig" Enabled="false" ForeColor="Gray" ToolTip="Download Web.Config" CommandName="Web.Config" PostBackUrl="~/GetZip.ashx?File=Web.Config" runat="server">Web.Config</asp:LinkButton>
15: <br />
16: <asp:ImageButton ID="ImageButtonBackupUpload" OnClick="ImageButtonBackupUpload_Click" ToolTip="Create UploadedFiles backup" runat="server" ImageUrl="~/images/plus.png" CommandName="UploadedFiles" />
17: <asp:ImageButton ID="ImageButtonDownloadUpload" ToolTip="Download UploadedFiles" PostBackUrl="~/GetZip.ashx?File=UploadedFiles" runat="server" ImageUrl="~/images/Down-gray.gif" Enabled="false" />
18: <asp:LinkButton ID="LinkButtonDownloadUpload" Enabled="false" ForeColor="Gray" ToolTip="Download UploadedFiles" CommandName="UploadedFiles" PostBackUrl="~/GetZip.ashx?File=UploadedFiles" runat="server">Uploaded Files</asp:LinkButton>
19: (*)<br />
20: <br />
21: <table border="0" cellpadding="1">
22: <asp:DataList runat="server" ID="DataList1">
23: <ItemTemplate>
24: <uc1:BackupTableItem runat="server" id="BackupTableItem1" />
25: </ItemTemplate>
26: </asp:DataList>
27: </table>
28: <span style="color:gray">(*) DB-Table.Zip saved to folder '~/App_Data/Backup', User Upload their data to '~/App_Data/UserID', this point create full backup of '~/App_Data/'</span>
29: <br /><asp:Label ID="Lerr1" runat="server" ForeColor="Red" Text=""></asp:Label>
30: </asp:Content>
1: '------------------------------------------------------------------------------
2: ' <auto-generated>
3: ' This code was generated by a tool.
4: '
5: ' Changes to this file may cause incorrect behavior and will be lost if
6: ' the code is regenerated.
7: ' </auto-generated>
8: '------------------------------------------------------------------------------
9:
10: Option Strict On
11: Option Explicit On
12:
13:
14: Partial Public Class Backup1
15:
16: '''<summary>
17: '''DelImageButton control.
18: '''</summary>
19: '''<remarks>
20: '''Auto-generated field.
21: '''To modify move field declaration from designer file to code-behind file.
22: '''</remarks>
23: Protected WithEvents DelImageButton As Global.System.Web.UI.WebControls.ImageButton
24:
25: '''<summary>
26: '''DelLinkButton control.
27: '''</summary>
28: '''<remarks>
29: '''Auto-generated field.
30: '''To modify move field declaration from designer file to code-behind file.
31: '''</remarks>
32: Protected WithEvents DelLinkButton As Global.System.Web.UI.WebControls.LinkButton
33:
34: '''<summary>
35: '''ImageButtonBackupConfig control.
36: '''</summary>
37: '''<remarks>
38: '''Auto-generated field.
39: '''To modify move field declaration from designer file to code-behind file.
40: '''</remarks>
41: Protected WithEvents ImageButtonBackupConfig As Global.System.Web.UI.WebControls.ImageButton
42:
43: '''<summary>
44: '''ImageButtonDownloadConfig control.
45: '''</summary>
46: '''<remarks>
47: '''Auto-generated field.
48: '''To modify move field declaration from designer file to code-behind file.
49: '''</remarks>
50: Protected WithEvents ImageButtonDownloadConfig As Global.System.Web.UI.WebControls.ImageButton
51:
52: '''<summary>
53: '''LinkButtonDownloadConfig control.
54: '''</summary>
55: '''<remarks>
56: '''Auto-generated field.
57: '''To modify move field declaration from designer file to code-behind file.
58: '''</remarks>
59: Protected WithEvents LinkButtonDownloadConfig As Global.System.Web.UI.WebControls.LinkButton
60:
61: '''<summary>
62: '''ImageButtonBackupUpload control.
63: '''</summary>
64: '''<remarks>
65: '''Auto-generated field.
66: '''To modify move field declaration from designer file to code-behind file.
67: '''</remarks>
68: Protected WithEvents ImageButtonBackupUpload As Global.System.Web.UI.WebControls.ImageButton
69:
70: '''<summary>
71: '''ImageButtonDownloadUpload control.
72: '''</summary>
73: '''<remarks>
74: '''Auto-generated field.
75: '''To modify move field declaration from designer file to code-behind file.
76: '''</remarks>
77: Protected WithEvents ImageButtonDownloadUpload As Global.System.Web.UI.WebControls.ImageButton
78:
79: '''<summary>
80: '''LinkButtonDownloadUpload control.
81: '''</summary>
82: '''<remarks>
83: '''Auto-generated field.
84: '''To modify move field declaration from designer file to code-behind file.
85: '''</remarks>
86: Protected WithEvents LinkButtonDownloadUpload As Global.System.Web.UI.WebControls.LinkButton
87:
88: '''<summary>
89: '''DataList1 control.
90: '''</summary>
91: '''<remarks>
92: '''Auto-generated field.
93: '''To modify move field declaration from designer file to code-behind file.
94: '''</remarks>
95: Protected WithEvents DataList1 As Global.System.Web.UI.WebControls.DataList
96:
97: '''<summary>
98: '''Lerr1 control.
99: '''</summary>
100: '''<remarks>
101: '''Auto-generated field.
102: '''To modify move field declaration from designer file to code-behind file.
103: '''</remarks>
104: Protected WithEvents Lerr1 As Global.System.Web.UI.WebControls.Label
105: End Class
1: Imports System.Data.SqlClient
2: Imports System.IO
3: Imports System.Xml
4:
5: Public Class Backup1
6: Inherits System.Web.UI.Page
7:
8: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
9: If Not IsPostBack Then
10: Dim DB1 As New ProgrammerExpertDBContext
11: Dim Tables = DB1.Database.SqlQuery(Of String)("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where @p=@p order by TABLE_Name", New SqlParameter("@p", "1")).ToList()
12: DataList1.DataSource = Tables
13: DataList1.DataBind()
14: End If
15: End Sub
16:
17: Private Sub DataList1_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles DataList1.ItemDataBound
18: If e.Item.DataItem IsNot Nothing Then
19: Dim CurBackupTableItem As BackupTableItem = TryCast(e.Item.Controls(1), BackupTableItem)
20: Dim ImageButton1 As ImageButton = CurBackupTableItem.FindControl("ImageButton1")
21: Dim ImageButton2 As ImageButton = CurBackupTableItem.FindControl("ImageButton2")
22: Dim LinkButton1 As LinkButton = CurBackupTableItem.FindControl("LinkButton1")
23: ImageButton1.CommandName = e.Item.DataItem
24: ImageButton2.PostBackUrl = "~/GetZip.ashx?File=" & e.Item.DataItem
25: LinkButton1.CommandName = e.Item.DataItem
26: LinkButton1.PostBackUrl = "~/GetZip.ashx?File=" & e.Item.DataItem
27: LinkButton1.Text = e.Item.DataItem
28: End If
29: End Sub
30:
31: Protected Sub DelImageButton_Click(sender As Object, e As ImageClickEventArgs) Handles DelImageButton.Click
32: ClearFolder()
33: End Sub
34:
35: Protected Sub DelLinkButton_Click(sender As Object, e As EventArgs) Handles DelLinkButton.Click
36: ClearFolder()
37: End Sub
38:
39: Private Sub ClearFolder()
40: Dim RootDir As String = Server.MapPath("~/App_Data/Backup")
41: Dim di As System.IO.DirectoryInfo = New DirectoryInfo(RootDir)
42:
43: For Each file As FileInfo In di.GetFiles()
44: file.Delete()
45: Next
46:
47: For Each dir As DirectoryInfo In di.GetDirectories()
48: dir.Delete(True)
49: Next
50: End Sub
51:
52: Protected Sub ImageButtonBackupConfig_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButtonBackupConfig.Click
53: Try
54: Dim OutDataDir As String = Server.MapPath("~/App_Data/Backup/")
55: Dim OutFileName As String = OutDataDir & "web.config.zip"
56: Dim InFileName As String = Server.MapPath("~/web.config")
57: Zip.AddFileToZip(OutFileName, InFileName)
58: ImageButtonDownloadConfig.Enabled = True
59: ImageButtonDownloadConfig.ImageUrl = "~/images/Down-green.png"
60: LinkButtonDownloadConfig.ForeColor = System.Drawing.Color.Black
61: LinkButtonDownloadConfig.Enabled = True
62: Catch ex As Exception
63: Lerr1.Text = ex.Message
64: End Try
65:
66: End Sub
67:
68: Protected Sub ImageButtonBackupUpload_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButtonBackupUpload.Click
69: Try
70: Dim OutDataDir As String = Server.MapPath("~/App_Data/Backup/")
71: Dim UploadDir As String = Server.MapPath("~/App_Data/") 'recursion
72: Dim OutFileName As String = System.IO.Path.Combine(OutDataDir, "UP_" & ImageButtonBackupUpload.CommandName & ".zip")
73: Dim CN = New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
74: CN.Open()
75: Zip.ZipFolder(OutFileName, UploadDir)
76: ImageButtonBackupUpload.Enabled = True
77: ImageButtonBackupUpload.ImageUrl = "~/images/Down-green.png"
78: LinkButtonDownloadUpload.ForeColor = System.Drawing.Color.Black
79: LinkButtonDownloadUpload.Enabled = True
80: Catch ex As Exception
81: Lerr1.Text = ex.Message
82: End Try
83:
84: End Sub
85:
86: End Class