使用VB.Net向Microsoft Access数据库添加新记录?

时间:2014-03-29 22:40:32

标签: vb.net ms-access-2010

我有一个名为(Registration)的表单,以及一个名为(UsersLogin)的数据库,其中包含一个名为(tbl_user)的表,其中包含字段

  • (如first_name)
  • (姓氏)
  • (middle_name)
  • (年龄)
  • (地址)
  • (用户名)
  • (密码)。

我希望不仅能够编辑记录(我已经可以这样做了),但我似乎无法添加/创建新的行/记录。以下是我目前使用的registration课程更新数据库行,但我似乎无法添加'新纪录(而非简单更新):

Imports System.Data
Imports System.Data.SqlClient
Public Class Registration
    Private Sub Registration_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.Tbl_userTableAdapter.Fill(Me.UsersLoginDataSet.tbl_user)
        Catch ex As Exception
            MessageBox.Show("The database file is unavailable", "Database Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.Close()
        End Try
    End Sub

    Private Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click
        Try
            Me.Validate()
            Me.TbluserBindingSource.EndEdit()
            Me.Tbl_userTableAdapter.Update(Me.UsersLoginDataSet)
            MessageBox.Show("Updates to the database have been successful.", "Successful Updates", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show("Updates to the database have failed.", "Unsuccessful Updates", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        LoginScreen.Show()
        Me.Hide()
    End Sub
End Class

数据集名为UsersLoginDataSet.xsd

任何人都知道如何添加新用户(即创建一个新行),并将其写入表中?

我已经在网上寻找了几个解决方案,但是无法添加新行,因此"填充"数据库中的记录。

有没有人可以解释这个应该如何完成?

1 个答案:

答案 0 :(得分:3)

您提供的详细信息还不够,但我会根据您的评论尝试回答。以下是MSDN中有关如何添加新行的示例。创建新行时,必须在表格上使用NewRow(),创建后使用表格上的Rows.Add()添加新行:

Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow()

newCustomersRow("CustomerID") = "ALFKI"
newCustomersRow("CompanyName") = "Alfreds Futterkiste"

DataSet1.Tables("Customers").Rows.Add(newCustomersRow)

请填写更多详情,请阅读MSDN article

相关问题