重要编辑
我解决了这个问题。实际上这不是问题。发生的事情是当我调试程序时,一个新的.mdf文件在调试文件夹中创建,我的Datagridview和应用程序中的其他对象从中获取信息。但我使用的还原查询是在我的解决方案文件夹(主.mdf文件)上恢复数据库,所以每次我完成操作时,解决方案文件都会恢复并在下次调试时显示,因为它重新创建了.mdf文件解决方案文件夹中的调试文件夹。因此可以查看下一次调试。
我已经创建了一个内置的备份/恢复应用程序,我可以备份&恢复成功但成功恢复后我必须关闭应用程序并重新启动它以查看更改,但我不想关闭应用程序,而是希望它在成功恢复后立即显示更改,这样可能吗?如果是,那怎么样?
当前代码:
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
表格 - 测试
我使用bindingsource来填充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)
答案 0 :(得分:1)
您可以在恢复数据库的同时调用bindingsource来填充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
答案 1 :(得分:0)
我想你必须在成功恢复后重新填充数据网格
寻找评论
//成功恢复后重新填充数据网格
在我的回答中
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()
// Refill the datagrid after succesfull restore
Me.StudentsTableAdapter.Fill(Me.BIS.Students)
MsgBox("Restore Successfull!")
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
编辑:尝试的事情 在任何地方的表单上放一个按钮,并在其中尝试此代码:
yourDataGridView.BindingSource = null
Me.StudentsTableAdapter.Fill(Me.BIS.Students)
yourDataGridView.BindingSource = Me.StudentsBindingSource
Me.StudentsBindingSource.ResetBindings(False)
编辑:使用相同的连接填充命令 (对不起,这是在c#我不知道visual basic)
SqlCommand command = new SqlCommand("your query", con2);
command.CommandType = Command.Text;
SqlAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(Me.BIS.Students);
编辑:用户查询的不同数据库:
SqlCommand command = new SqlCommand("use BISDB " + your query, con2);