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 而不是我希望它在成功恢复后立即显示更改,这可能吗?如果是那么如何?
当前代码:
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
表格 - 测试
我使用绑定(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/