使用 VB.Net 向 Microsoft Access 数据库添加新记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22737851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a New Record to Microsoft Access Database using VB.Net?
提问by jbutler483
I have a form called (Registration), and a database called (UsersLogin) with a table called (tbl_user) with the fields
我有一个名为 (Registration) 的表单和一个名为 (UsersLogin) 的数据库,其中有一个名为 (tbl_user) 的表,其中包含字段
- (first_name)
- (last_name)
- (middle_name)
- (age)
- (address)
- (username)
- (password).
- (名)
- (姓)
- (中间名字)
- (年龄)
- (地址)
- (用户名)
- (密码)。
I want to not only be able to edit records (I can already do this), but I cant seem to be able to add/create a new row/record. Below is the class registrationI am using so far to updatea database row, but I can't seem to be able to 'add' a new record (rather than simply updating):
我不仅希望能够编辑记录(我已经可以做到这一点),而且我似乎无法添加/创建新行/记录。下面是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
The dataset is called UsersLoginDataSet.xsd
该数据集称为 UsersLoginDataSet.xsd
Anyone know how to add a new user (i.e. make a new row), and hence write this to a table?
任何人都知道如何添加一个新用户(即创建一个新行),从而将其写入表中?
I have looked for several solutions to this on the web, but have not been able to add a new row and hence "fill" the record in the database.
我在网上寻找了几种解决方案,但一直无法添加新行并因此“填充”数据库中的记录。
Could anyone possibly explain how this shouldbe done?
谁能解释一下这应该怎么做?
回答by Racil Hilan
The details you provided are not enough, but I will try to answer according to your comment. Below is a example from MSDN about how to add a new row. When you create the new row, you have to use NewRow()on the table, and after creating it use Rows.Add()on your table to add the new row:
您提供的详细信息还不够,但我会根据您的评论尝试回答。以下是 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)
Form more details, you read the MSDN article.
形成更多详细信息,您阅读MSDN 文章。

