如何使用数据集向数据库 VB.NET 添加记录?
How to add a record to a database VB.NET, using datasets?
我有一个包含三个 table 的 Microsoft Access 数据库 - "Class 1"、"Class 2" 和 "Class 3"。在每个 table 中,有三列:"ID"、"Name" 和 "Score"。在 Visual Basic 中,我有两个变量——名称和分数。我想使用数据集在数据库的新行中添加名称和分数。看了msdn的教程,其实不是很懂:
https://msdn.microsoft.com/en-us/library/5ycd1034.aspx
Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
newCustomersRow.CustomerID = "ALFKI"
newCustomersRow.CompanyName = "Alfreds Futterkiste"
NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)
"NorthwindDataSet1" 从何而来?我知道 "NorthwinDataSet" 的来源 = 那是通过数据源选项卡创建的数据集的名称。
我不确定如何将记录添加到 table。我是这样开始的:
Dim newScoreRow As scoresDataSet.Class_1Row
我不知道下一行代码怎么写,因为我看不懂MSDN文档中的第二行代码。
第二行代码写完后,我明白我写的是这样的:
newScoreRow.Name = name
newScoreRow.Score = Str(Score)
综上所述,"NorthwindDataSet1"从何而来?我将如何完成我的代码?
NorthwindDataSet1
是名为 NorthWindDataSet
的类型化数据集的一个实例。看看这些 MSDN 页面 Working with Datasets in Visual Studio and How to: Create a Typed Dataset
在第二个 link 之后,我在上面 post 创建了一个名为 scoresDataSet
的新数据集,并添加了一个名为 Class_1
的 table 并在table 我创建了 ID
、Name
和 Score
列,就像您在问题中所做的那样。
为我生成的类型化数据集 Class 文件在 SO 上变大到 post,但这是我用来向 [= 添加新行的代码数据集中的 52=] table。 addNewRowButton
只是表单上的一个按钮。我还在表单上添加了两个文本框 nameTextBox
和 scoreTextbox
.
Private ScoresDataSet1 As New scoresDataSet()
Private Sub addNewRowButton_Click(sender As Object, e As EventArgs) Handles addNewRowButton.Click
Dim name As String = nameTextBox.Text
Dim score As String = scoreTextbox.Text
Dim newScoreRow As scoresDataSet.Class_1Row
newScoreRow = ScoresDataSet1.Class_1.NewClass_1Row
newScoreRow.Name = name
newScoreRow.Score = score
ScoresDataSet1.Class_1.Rows.Add(newScoreRow)
End Sub
这里的技巧是您的数据实际上存在于 Access 数据库中,因此您需要使用 Data Source Configuration Wizard 并选择将数据源添加到您的项目,而不是像我那样手动创建数据集Microsoft Access Database File
。
在visual Studio2012年,只需前往:
- 项目
- 添加新数据源...
- 数据库
- 数据集
- 新连接...
- 改变...
并选择适当的选项并填写 windows 中的其他字段。完成并构建项目后,您应该在表单设计器的应用程序组件下的工具箱中有一个新的 DataSet 对象和关联的 TableAdapters。
将新创建的数据集和关联的 TableAadapter 的副本添加到表单后,您可以使用 TableAdapter 将数据从访问数据库填充到数据集中,并且可以使用 TableAdapter 将数据存储到访问数据库中。
我有一个包含三个 table 的 Microsoft Access 数据库 - "Class 1"、"Class 2" 和 "Class 3"。在每个 table 中,有三列:"ID"、"Name" 和 "Score"。在 Visual Basic 中,我有两个变量——名称和分数。我想使用数据集在数据库的新行中添加名称和分数。看了msdn的教程,其实不是很懂:
https://msdn.microsoft.com/en-us/library/5ycd1034.aspx
Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
newCustomersRow.CustomerID = "ALFKI"
newCustomersRow.CompanyName = "Alfreds Futterkiste"
NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)
"NorthwindDataSet1" 从何而来?我知道 "NorthwinDataSet" 的来源 = 那是通过数据源选项卡创建的数据集的名称。
我不确定如何将记录添加到 table。我是这样开始的:
Dim newScoreRow As scoresDataSet.Class_1Row
我不知道下一行代码怎么写,因为我看不懂MSDN文档中的第二行代码。
第二行代码写完后,我明白我写的是这样的:
newScoreRow.Name = name
newScoreRow.Score = Str(Score)
综上所述,"NorthwindDataSet1"从何而来?我将如何完成我的代码?
NorthwindDataSet1
是名为 NorthWindDataSet
的类型化数据集的一个实例。看看这些 MSDN 页面 Working with Datasets in Visual Studio and How to: Create a Typed Dataset
在第二个 link 之后,我在上面 post 创建了一个名为 scoresDataSet
的新数据集,并添加了一个名为 Class_1
的 table 并在table 我创建了 ID
、Name
和 Score
列,就像您在问题中所做的那样。
为我生成的类型化数据集 Class 文件在 SO 上变大到 post,但这是我用来向 [= 添加新行的代码数据集中的 52=] table。 addNewRowButton
只是表单上的一个按钮。我还在表单上添加了两个文本框 nameTextBox
和 scoreTextbox
.
Private ScoresDataSet1 As New scoresDataSet()
Private Sub addNewRowButton_Click(sender As Object, e As EventArgs) Handles addNewRowButton.Click
Dim name As String = nameTextBox.Text
Dim score As String = scoreTextbox.Text
Dim newScoreRow As scoresDataSet.Class_1Row
newScoreRow = ScoresDataSet1.Class_1.NewClass_1Row
newScoreRow.Name = name
newScoreRow.Score = score
ScoresDataSet1.Class_1.Rows.Add(newScoreRow)
End Sub
这里的技巧是您的数据实际上存在于 Access 数据库中,因此您需要使用 Data Source Configuration Wizard 并选择将数据源添加到您的项目,而不是像我那样手动创建数据集Microsoft Access Database File
。
在visual Studio2012年,只需前往:
- 项目
- 添加新数据源...
- 数据库
- 数据集
- 新连接...
- 改变...
并选择适当的选项并填写 windows 中的其他字段。完成并构建项目后,您应该在表单设计器的应用程序组件下的工具箱中有一个新的 DataSet 对象和关联的 TableAdapters。
将新创建的数据集和关联的 TableAadapter 的副本添加到表单后,您可以使用 TableAdapter 将数据从访问数据库填充到数据集中,并且可以使用 TableAdapter 将数据存储到访问数据库中。