⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmdatabase.vb

📁 一个实际的商业项目
💻 VB
字号:
Imports System.Data.SqlClient
Imports System
Imports System.IO

Public Class frmDataBase
    Inherits System.Windows.Forms.Form

    Public ServerID As String
    Public databaseUser As String
    Public databasePass As String

    Dim WithEvents cnData As New SqlConnection
    Dim cmdSQL As New SqlCommand

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents btnBak As System.Windows.Forms.Button
    Friend WithEvents btnRes As System.Windows.Forms.Button
    Friend WithEvents btnExit As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.btnBak = New System.Windows.Forms.Button
        Me.btnRes = New System.Windows.Forms.Button
        Me.btnExit = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Font = New System.Drawing.Font("楷体_GB2312", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.TextBox1.Location = New System.Drawing.Point(32, 24)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(264, 176)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'btnBak
        '
        Me.btnBak.Location = New System.Drawing.Point(312, 32)
        Me.btnBak.Name = "btnBak"
        Me.btnBak.TabIndex = 1
        Me.btnBak.Text = "备份"
        '
        'btnRes
        '
        Me.btnRes.Location = New System.Drawing.Point(312, 72)
        Me.btnRes.Name = "btnRes"
        Me.btnRes.TabIndex = 2
        Me.btnRes.Text = "还原"
        '
        'btnExit
        '
        Me.btnExit.Location = New System.Drawing.Point(312, 176)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.TabIndex = 3
        Me.btnExit.Text = "退出"
        '
        'frmDataBase
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(408, 222)
        Me.Controls.Add(Me.btnExit)
        Me.Controls.Add(Me.btnRes)
        Me.Controls.Add(Me.btnBak)
        Me.Controls.Add(Me.TextBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmDataBase"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "数据库备份和恢复"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmDataBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "    为了防止意外的数据丢失,做好日常的数据备份非常重要。" & vbNewLine & _
                        "    一般每天做一次数据备份,为了节省磁盘空间,可以保留一周的数据,同时要注意把备份文件转移到其他的机器上,防止机器故障引起数据丢失,减少工作损失。"

        cnData.ConnectionString = _
                       "packet size=4096;" & _
                       "user id=" & databaseUser & ";password=" & databasePass & ";" & _
                       "data source=" & ServerID & ";" & _
                       "persist security info=False;" '& _
                       '"initial catalog=gongzi"

        cmdSQL.Connection = cnData
        TextBox1.Select(0, 0)

        btnBak.Focus()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnBak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBak.Click
        SaveFileDialog1.RestoreDirectory = True
        Dim strBak As String

        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            strBak = SaveFileDialog1.FileName
            File.Delete(strBak)
            File.Copy(Application.StartupPath & "\gongzi.mdb", strBak)


            MessageBox.Show("备份成功!")
        End If
        

    End Sub

    Private Sub btnRes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRes.Click
        OpenFileDialog1.RestoreDirectory = True
        Dim strRes As String

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            strRes = OpenFileDialog1.FileName
            File.Delete(Application.StartupPath & "\gongzi.mdb")
            File.Copy(strRes, Application.StartupPath & "\gongzi.mdb")
            MessageBox.Show("恢复成功!")
        End If
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -