问题 我有一个数据库连接到我的程序,我知道连接正确,因为在其他两种形式我可以访问数据库。在一种形式中,我将输入的登录详细信息与数据库进行比较,并且它工作正常,另一种形式的奇怪之处在于,我使用更新语句将新用户添加到表中,并且在关闭并重新启动应用程序后此数据仍然存在。当编辑dataveiw中的数据并单击保存按钮显示为已保存时,在此表单上澄清问题,但是当程序关闭并打开时,此数据仍然不存在。
守则
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Public Class FrmFinance
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim varsearch As New BindingSource()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MasterDBDataSet.Table_usr' table. You can move, or remove it, as needed.
Me.Table_usrTableAdapter.Fill(Me.MasterDBDataSet.Table_usr)
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Student\Documents\Visual Studio 2010\Projects\MasterDB.accdb"
myConnection.ConnectionString = connString
da = New OleDbDataAdapter("Select [Username], [Surname], [YearGroup] from Table_usr", myConnection)
da.Fill(ds, "Table_usr")
Dim searchveiw As New DataView(tables(0))
varsearch.DataSource = searchveiw
DataGridView1.DataSource = searchveiw
DataGridView1.Refresh()
End Sub
Private Sub ButtSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtSave.Click
Try
TableusrBindingSource.EndEdit()
Table_usrTableAdapter.Update(MasterDBDataSet.Table_usr)
MessageBox.Show("Data Saved")
Catch ex As Exception
MessageBox.Show("Error Saving Data")
End Try
End Sub
Private Sub ButtFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtFilter.Click
If Search_username.TextLength > 1 Then
varsearch.Filter = "[Username] = '" & Search_username.Text & "'"
DataGridView1.Refresh()
ElseIf Search_Surname.TextLength > 1 Then
varsearch.Filter = "[Surname] = '" & Search_Surname.Text & "'"
DataGridView1.Refresh()
ElseIf Search_YrGroup.TextLength > 1 Then
varsearch.Filter = "[YearGroup] = '" & Search_YrGroup.Text & "'"
DataGridView1.Refresh()
End If
End Sub
End Class
我包括了整个表格,以防问题出现在一些我不理解的冲突中。当我设置数据源时,我单击否将数据库复制到每次运行的bin,并且database.xcd设置为copy,如果更新仍然在关闭时丢失信息。非常感谢任何帮助。
答案 0 :(得分:0)
试试这个,
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
Try
Me.Validate()
TableusrBindingSource.EndEdit()
Table_usrTableAdapter.UpdateAll(Me.MasterDBDataSet.Table_usr)
MessageBox.Show("Data Saved")
Catch ex As Exception
MessageBox.Show("Error Saving Data")
End Try
End Sub