如何从vb.net添加到访问数据库

时间:2014-03-27 08:54:46

标签: database vb.net ms-access visual-studio-2013 database-connection

我还在学习VB和数据库,但我有一点问题。我的代码有效,但是当我  试着查看数据库我没有找到所有已保存的内容,我该如何纠正呢?还有编辑和删除。感谢

这是我的代码:

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
    Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
    'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
    Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
    'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
    Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)

End Sub

Private Sub ItemsDataSetBindingSource_CurrentChanged(sender As Object, e As EventArgs)

End Sub

Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click

    ItemsBindingSource.EndEdit()
    ItemsTableAdapter.Update(ItemsDataSet.Items)
    MsgBox("Saved")
End Sub

Private Sub btadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
    ItemsBindingSource.AddNew()
    MsgBox("Added Successfully")



End Sub

Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
    ItemsBindingSource.RemoveCurrent()
    MsgBox("Item Deleted")
End Sub



Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles btnexit.Click
    Close()

End Sub

Private Sub btnprevious_Click(sender As Object, e As EventArgs) Handles btnprevious.Click
    ItemsBindingSource.MovePrevious()
End Sub

Private Sub btnnext_Click(sender As Object, e As EventArgs) Handles btnnext.Click
    ItemsBindingSource.MoveNext()
End Sub

End Class

1 个答案:

答案 0 :(得分:0)

您的项目文件中可能包含MDB(ACCDB)文件。 如果检查与此项目文件关联的属性,则会找到一个名为“复制到输出目录”的属性。将其设置为“始终复制”。

此外,您的连接字符串包含快捷方式Data Source =" | DataDirectory | \ yourdb.mdb"

如果此方案正确,则启动应用程序,并将MDB / ACCDB文件从原始位置复制到BIN \ DEBUG目录。 您可以在那里插入数据而不会出错。当您停止调试会话以修复错误或其他问题时,请重新启动应用程序,并在输出目录中再次复制数据库的新副本(但为空)。

要解决此问题,请将属性设置为“从不复制”或将连接字符串更改为指向固定位置。