重要编辑
我解决了问题。实际上这不是问题。发生的是,当我调试程序时,在debug文件夹中创建了一个新的.mdf文件,我的Datagridview和应用程序中的其他对象从该文件夹中获取信息。但是我使用的还原查询是将数据库还原到我的解决方案文件夹(主.mdf文件)中,因此,每次完成操作后,都会还原该解决方案文件,并在下次调试时显示该文件,因为它会重新创建.mdf文件。解决方案文件夹中的debug文件夹。因此可以在下次调试时查看。
我创建了一个内置的“备份/还原”应用程序,该应用程序可以成功备份和还原,但是还原成功后,我必须关闭该应用程序,然后再次重新启动以查看更改,但我不想关闭该应用程序希望它在成功还原后立即显示更改,这可能吗?如果是,那怎么办?
当前代码:
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)
我想您必须在成功还原后重新填充数据网格。
寻找评论
//
在我的答案中成功恢复后重新填充数据网格
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句