📄 datatables.vb
字号:
Me.dsUntyped.DataSetName = "dsUntyped"
Me.dsUntyped.Locale = New System.Globalization.CultureInfo("en-US")
Me.dsUntyped.Tables.AddRange(New System.Data.DataTable() {Me.dtMaster, Me.dtChild})
'
'dtMaster
'
Me.dtMaster.Columns.AddRange(New System.Data.DataColumn() {Me.MasterID, Me.MasterValue})
Me.dtMaster.TableName = "dtMaster"
'
'MasterValue
'
Me.MasterValue.Caption = "MasterValue"
Me.MasterValue.ColumnName = "MasterValue"
'
'btnDataSet
'
Me.btnDataSet.Location = New System.Drawing.Point(416, 56)
Me.btnDataSet.Name = "btnDataSet"
Me.btnDataSet.Size = New System.Drawing.Size(96, 24)
Me.btnDataSet.TabIndex = 4
Me.btnDataSet.Text = "Dataset Table "
'
'lbEmployees
'
Me.lbEmployees.Location = New System.Drawing.Point(8, 24)
Me.lbEmployees.Name = "lbEmployees"
Me.lbEmployees.Size = New System.Drawing.Size(184, 95)
Me.lbEmployees.TabIndex = 1
'
'dgOrders
'
Me.dgOrders.DataMember = ""
Me.dgOrders.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgOrders.Location = New System.Drawing.Point(8, 152)
Me.dgOrders.Name = "dgOrders"
Me.dgOrders.Size = New System.Drawing.Size(392, 208)
Me.dgOrders.TabIndex = 3
'
'lblEmployees
'
Me.lblEmployees.AutoSize = True
Me.lblEmployees.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblEmployees.Location = New System.Drawing.Point(8, 8)
Me.lblEmployees.Name = "lblEmployees"
Me.lblEmployees.Size = New System.Drawing.Size(66, 13)
Me.lblEmployees.TabIndex = 0
Me.lblEmployees.Text = "Employees:"
'
'lblClients
'
Me.lblClients.AutoSize = True
Me.lblClients.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblClients.Location = New System.Drawing.Point(200, 8)
Me.lblClients.Name = "lblClients"
Me.lblClients.Size = New System.Drawing.Size(44, 13)
Me.lblClients.TabIndex = 0
Me.lblClients.Text = "Clients:"
'
'dsMaster1
'
Me.dsMaster1.DataSetName = "dsMaster"
Me.dsMaster1.Locale = New System.Globalization.CultureInfo("en-US")
Me.dsMaster1.Namespace = "http://www.tempuri.org/dsMaster.xsd"
'
'btnTable
'
Me.btnTable.Location = New System.Drawing.Point(416, 24)
Me.btnTable.Name = "btnTable"
Me.btnTable.Size = New System.Drawing.Size(96, 24)
Me.btnTable.TabIndex = 5
Me.btnTable.Text = "Add Table"
'
'frmDataSets
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(520, 373)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnTable, Me.btnSelect, Me.btnUnique, Me.btnForeign, Me.btnSchema, Me.btnVersion, Me.btnAddRow, Me.btnCalculate, Me.btnDataSet, Me.dgOrders, Me.lbClients, Me.lbEmployees, Me.lblOrders, Me.lblClients, Me.lblEmployees})
Me.Name = "frmDataSets"
Me.Text = "DataTables"
CType(Me.dtChild, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dsUntyped, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dtMaster, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dgOrders, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dsMaster1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTable.Click
Dim strMessage As String
'Create the table
Dim dtEmployees As New System.Data.DataTable("Employees")
strMessage = "The table name is "
strMessage &= dtEmployees.TableName.ToString
MessageBox.Show(strMessage)
End Sub
Private Sub btnDataSet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDataSet.Click
Dim strMessage As String
'Create the table
Me.dsEmployees.Tables.Add()
strMessage = "The table name is "
strMessage &= Me.dsEmployees.Tables(0).TableName.ToString
MessageBox.Show(strMessage)
End Sub
Private Sub btnSchema_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSchema.Click
Dim strMessage As String
Me.dsEmployees.Tables.Add("Employees")
Me.daEmployees.FillSchema(Me.dsEmployees.Tables("Employees"), _
SchemaType.Source)
With Me.dsEmployees.Tables("Employees")
strMessage = "Primary Key: "
strMessage &= .PrimaryKey(0).ColumnName.ToString
strMessage &= vbCrLf & "Constraint Count: "
strMessage &= .Constraints(0).ConstraintName.ToString
MessageBox.Show(strMessage)
End With
End Sub
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim dcName As System.Data.DataColumn
'Create the table
Me.dsEmployees.Tables.Add("Employees")
'Fill the table from daEmployees
Me.daEmployees.Fill(Me.dsEmployees.Tables(0))
'Create the column
dcName = New System.Data.DataColumn("Name")
dcName.DataType = System.Type.GetType("System.String")
dcName.Expression = "FirstName + ' ' + LastName"
'Add the calculated column
Me.dsEmployees.Tables("Employees").Columns.Add(dcName)
'Bind to the listbox
Me.lbEmployees.DataSource = Me.dsEmployees.Tables("Employees")
Me.lbEmployees.DisplayMember = "Name"
End Sub
Private Sub btnAddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
Dim drNew As System.Data.DataRow
'Create the new row
drNew = Me.dsMaster1.CustomerList.NewRow
drNew.Item("CustomerID") = "ANEWR"
drNew.Item("CompanyName") = "A New Row"
'Add row to table
Me.dsMaster1.CustomerList.Rows.Add(drNew)
'Refresh the display
Me.lbClients.Refresh()
End Sub
Private Sub btnVersion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVersion.Click
Dim strMessage As String
With Me.dsMaster1.CustomerList.Rows(0)
.Item("CustomerID") = "NEWVAL"
strMessage = "The RowState is " & .RowState.ToString
strMessage &= vbCrLf & "The original value was "
strMessage &= .Item("CustomerID", DataRowVersion.Original)
strMessage &= vbCrLf & "The new value is "
strMessage &= .Item("CustomerID", DataRowVersion.Current)
End With
MessageBox.Show(strMessage)
End Sub
Private Sub btnForeign_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnForeign.Click
Dim strMessage As String
Dim fkNew As System.Data.ForeignKeyConstraint
With Me.dsUntyped
fkNew = New System.Data.ForeignKeyConstraint("NewFK", _
.Tables("dtMaster").Columns("MasterID"), _
.Tables("dtChild").Columns("MasterLink"))
.Tables("dtChild").Constraints.Add(fkNew)
strMessage = "The new constraint is called "
strMessage &= .Tables("dtChild").Constraints(0).ConstraintName.ToString
End With
MessageBox.Show(strMessage)
End Sub
Private Sub btnUnique_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUnique.Click
Dim strMessage As String
Dim ucNew As System.Data.UniqueConstraint
With Me.dsUntyped.Tables("dtMaster")
ucNew = New System.Data.UniqueConstraint("NewUnique", _
.Columns("MasterValue"))
.Constraints.Add(ucNew)
strMessage = "The new constraint is called "
strMessage &= .Constraints("NewUnique").ConstraintName.ToString
End With
MessageBox.Show(strMessage)
End Sub
Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelect.Click
Dim drFound() As System.Data.DataRow
Dim dr As System.Data.DataRow
drFound = Me.dsMaster1.CustomerList.Select("CustomerID LIKE 'A*'")
Me.lbClients.DataSource = Nothing
Me.lbClients.Items.Clear()
For Each dr In drFound
Me.lbClients.Items.Add(dr("CompanyName"))
Next
Me.lbClients.Refresh()
End Sub
Private Sub lbClients_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbClients.SelectedIndexChanged
Dim drCurrent As System.Data.DataRow
Dim dsCustOrders As New System.Data.DataSet()
Dim drCustOrders() As System.Data.DataRow
'Create the relation if necessary
If Me.dsMaster1.Relations.Count = 0 Then
Me.dsMaster1.Relations.Add("CustomerOrders", _
Me.dsMaster1.CustomerList.CustomerIDColumn, _
Me.dsMaster1.OrderTotals.CustomerIDColumn)
End If
drCurrent = Me.lbClients.SelectedItem.Row
dsCustOrders.Merge(drCurrent.GetChildRows("CustomerOrders"))
Me.dgOrders.SetDataBinding(dsCustOrders, "OrderTotals")
Me.dgOrders.Refresh()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -