vb.net - 如何在不使用 Vb.Net 重新启动应用程序的情况下从 .bak 恢复数据库时刷新 Datagridview?

标签 vb.net datagridview

Important Edit

I solved the problem. It was not a problem actually. What was happening is when I debug the program a new .mdf file creates in the debug folder from which my Datagridview and other objects in the application gets information. But the restore query I used was restoring the database on my solution folder (the main .mdf file) so each time I finish my operation the solution file gets restored and showed on the next time I debug as it re create the .mdf file on the debug folder from the solution folder. And hence could view then on the next debug.

我已经创建了一个内置的备份/恢复应用程序,我能够成功备份和恢复,但是当成功恢复时我必须关闭应用程序并再次重新启动它以查看更改但我不想关闭app 而不是我希望它在成功恢复后立即显示更改,这可能吗?如果是那么如何?

表格截图: Screenshot of the form

当前代码:

Imports Microsoft.SqlServer.Server
Imports MySql.Data.MySqlClient
Imports System.Data.SqlClient

Public Class Test


    Sub Backup()
        With SaveFileDialog1
            .FileName = ""
            .Filter = "Backup File|*.bak"
            SaveFileDialog1.ShowDialog()
            If .FileName = "" Then
                Exit Sub
            Else
                Dim BUDate As New TextBox
                BUDate.Text = "Backup database BISDB To Disk='" + SaveFileDialog1.FileName + "'"
                Dim con As New SqlClient.SqlConnection("data source=.\SQLEXPRESS;initial catalog=BISDB;Integrated Security=True")
                Dim cmd As New SqlCommand()

                Try
                    con.Open()
                    cmd.CommandType = CommandType.Text
                    cmd.CommandText = BUDate.Text
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
                    MsgBox("Backup successfull!", MsgBoxStyle.OkOnly, "Backup")
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End With


    End Sub

    Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.StudentsTableAdapter.Fill(Me.BIS.Students)

    End Sub

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Backup()
    End Sub

    Private Sub btnRestore_Click(sender As Object, e As EventArgs) Handles btnRestore.Click
        With OpenFileDialog1
            .FileName = ""
            .Filter = "Backup File|*.bak"
            OpenFileDialog1.ShowDialog()
            If .FileName = "" Then
                Exit Sub
            Else
                Dim FileName As String = OpenFileDialog1.FileName
                Dim BUDate As New TextBox
                BUDate.Text = "RESTORE DATABASE BISDB FROM Disk='" + FileName + "'"
                Dim con2 As New SqlClient.SqlConnection("data source=.\SQLEXPRESS;initial catalog=master;Integrated Security=True")
                Dim cmd = New SqlCommand(BUDate.Text, con2)

                Try
                    con2.Open()
                    cmd.ExecuteNonQuery()
                    MsgBox("Restore Successfull!")
                    Me.StudentsTableAdapter.Fill(Me.BIS.Students) '//Edited According to Vignesh Kumar's Answer
                    Me.StudentsBindingSource.ResetBindings(False) '//Edited According to GuidoG's Answer
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    con2.Close()
                    con2.Dispose()

                End Try
            End If
        End With
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        'Me.BIS.Reset()
        'Me.StudentsBindingSource.Dispose()
        'Me.StudentsBindingSource.ResetAllowNew()
        Me.StudentsBindingSource.ResetBindings(True)
        Me.StudentsBindingSource.SuspendBinding()
        Me.StudentsBindingSource.ResumeBinding()

        Me.StudentsTableAdapter.Fill(Me.BIS.Students)
    End Sub
End Class

项目名称:

刷新按钮 - btnClose

备份按钮 - btnCalculate

恢复按钮 - btnRestore

Datagridview - DataGridView1

表格 - 测试

我通过单击开始按钮启动应用程序: enter image description here

我使用绑定(bind)源来填充 DataGridView 并且不使用任何代码来填充 DataGridView 但是 Me.StudentsTableAdapter.Fill(Me.BIS.Students) 加载表单时。

这是我到目前为止得到的:

Dim command = New SqlCommand("", con2)
        command.CommandType = CommandType.Text
        Dim adapter As New SqlDataAdapter(command)
        DataGridView1.DataSource = adapter
        adapter.Fill(Me.BIS.Students)

最佳答案

您可以在恢复数据库时调用绑定(bind)源来填充DataGridView

Me.StudentsTableAdapter.Fill(Me.BIS.Students) 

尝试如下:

  Private Sub
        With OpenFileDialog1
            .FileName = ""
            .Filter = "Backup File|*.bak"
            OpenFileDialog1.ShowDialog()
            If .FileName = "" Then
                Exit Sub
            Else
                Dim FileName As String = OpenFileDialog1.FileName
                Dim BUDate As New TextBox
                BUDate.Text = "RESTORE DATABASE BISDB FROM Disk='" + FileName + "'"
                Dim con2 As New SqlClient.SqlConnection("data source=.\SQLEXPRESS;initial catalog=master;Integrated Security=True")
                Dim cmd = New SqlCommand(BUDate.Text, con2)

                Try
                    con2.Open()
                    cmd.ExecuteNonQuery()
                    MsgBox("Restore Successfull!")
                    ' DataGrdiview Binding Code
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    con2.Close()
                    con2.Dispose()

                End Try
            End If
        End With
   End Sub

关于vb.net - 如何在不使用 Vb.Net 重新启动应用程序的情况下从 .bak 恢复数据库时刷新 Datagridview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182193/

相关文章:

vb.net - 变量在赋值之前已经被使用

c# - 访问控件: Nested repeater

vb.net - 使用 VB.NET 计算 Word 文档的字数

c# - 如何按 2 列对 datagridview 进行排序

c# - WinForm 尝试提交来自 CellValueChanged 事件的 DataGridView 更改

vb.net - 将字符串转换为日期时间

c# - LINQ-to-XML 到 DataGridView : Cannot edit fields -- How to fix?

c# - C#中如何从DataGridView中获取选中的行内容?

c# - 在 DataGridView 中将最后一行设置为 Frozen

vb.net - 高质量完整屏幕截图 VB.Net