我创建了一个需要以编程方式生成大约100到1000个对象的应用程序。将在数据库中使用相同的数量生成此对象。为了实现上述目标,我当然需要从数据库中调用数据,然后循环遍历每个数据以生成每个对象。但它需要这么长时间才能完成。我希望除了我这样做之外你还有另一种方法。
以下是我从数据库调用数据的代码
Dim tbls As New DataTable
Using cons As New SqlConnection(stringconnection)
Dim sqls = "select * from tbsectiondetail where IDSection = '" & txtIDSection.Text &
"' AND KodeLocation like '%" & txtCari.Text &
"%' AND Status like '%" & cbStatus.Text &
"%' ORDER BY Status DESC"
Dim adps As New SqlDataAdapter(sqls, cons)
adps.Fill(tbls)
adps.Dispose()
cons.Dispose()
End Using
如果我只生成数据,它只需要1到2秒。所以我不认为我如何调用数据库中的数据
以下是我通过循环数据生成对象的方法
Dim resultsx As DataRow() = tbls.Select("")
If resultsx.Count > 0 Then
For iX = 0 To resultsx.Count - 1
'CREATE A VARIABLE
Dim IDSectionDetail = resultsx(iX)("IDSectionDetail").ToString
Dim NamaLocation = resultsx(iX)("NamaLocation").ToString
Dim SectionKode = resultsx(iX)("KodeLocation").ToString
Dim StatusSection = resultsx(iX)("Status").ToString
Dim StatusSectionAt = resultsx(iX)("StatusAt").ToString
Dim StatusC1 = resultsx(iX)("StatusC1").ToString
Dim StatusC2 = resultsx(iX)("StatusC2").ToString
'GENERATE A PANEL
Dim pnlContainer As New Panel
pnlContainer.Size = New Size(290, 200)
Tablex.Controls.Add(pnlContainer, curColumn, rowNo)
pnlContainer.Dock = DockStyle.Fill
Dim pnlHeader As New Panel
pnlContainer.Controls.Add(pnlHeader)
pnlHeader.Dock = DockStyle.Top
pnlHeader.Size = New Size(pnlContainer.Width, 35)
If StatusSection = "Open" Then
pnlHeader.BackColor = Color.DarkSlateGray
Else
pnlHeader.BackColor = Color.FromArgb(185, 58, 50)
End If
pnlContainer.Controls.SetChildIndex(pnlHeader, 2)
'GENERATE LABEL HEADER
Dim lblHeader As New Label
lblHeader.Text = NamaLocation & " - " & SectionKode
pnlHeader.Controls.Add(lblHeader)
lblHeader.Font = New Font("Century Gothic", 9, FontStyle.Bold Or FontStyle.Italic)
lblHeader.ForeColor = Color.White
lblHeader.Location = New Point(8, 7)
lblHeader.AutoSize = True
'GENERATE LABEL STATUS
Dim lblStatus As New Label
lblStatus.Text = "Status : " & StatusSection
pnlHeader.Controls.Add(lblStatus)
lblStatus.Font = New Font("Century Gothic", 8, FontStyle.Bold Or FontStyle.Italic)
lblStatus.ForeColor = Color.White
lblStatus.TextAlign = ContentAlignment.MiddleRight
lblStatus.Height = pnlHeader.Height
lblStatus.Dock = DockStyle.Right
lblStatus.AutoSize = True
'GENERATE PANEL FOOTER
Dim pnlFooter As New Panel
pnlContainer.Controls.Add(pnlFooter)
pnlFooter.Dock = DockStyle.Fill
pnlFooter.Size = New Size(pnlFooter.Width, 35)
pnlFooter.BackColor = Color.White
pnlContainer.Controls.SetChildIndex(pnlFooter, 0)
'GENERATE BUTTON DETAIL
Dim btnDetail As New Button
pnlFooter.Controls.Add(btnDetail)
btnDetail.Name = "btn" & SectionKode
btnDetail.FlatStyle = FlatStyle.Flat
btnDetail.FlatAppearance.BorderColor = Color.FromArgb(46, 74, 98)
btnDetail.FlatAppearance.BorderSize = 1
btnDetail.FlatAppearance.MouseDownBackColor = Color.Gold
btnDetail.FlatAppearance.MouseOverBackColor = Color.Gold
btnDetail.BackColor = Color.DarkSlateGray
btnDetail.Text = "Detail"
btnDetail.Image = My.Resources.search2_icon
btnDetail.Font = New Font("Century Gothic", 8, FontStyle.Bold)
btnDetail.TextImageRelation = TextImageRelation.TextBeforeImage
btnDetail.Location = New Point(181, 84)
btnDetail.Anchor = AnchorStyles.Right And AnchorStyles.Top
btnDetail.Size = New Size(75, 28)
btnDetail.ForeColor = Color.White
AddHandler btnDetail.Click, Sub() DetailSection(IDSectionDetail, SectionKode)
curColumn += 1
If curColumn >= 5 Then
curColumn = 0
RowCount += 1
Tablex.RowCount = RowCount
rowNo += 1
End If
Next
End If
pnlFill.Controls.Add(Tablex)
实际上还有更多的对象可以通过循环生成。但是根据我的分析,循环已经花费了很长时间,只是来自上面的代码。
The result of the generated object
我只是想知道是否还有另一种更快的替代方法来生成这样的对象。或者我是否需要放弃设计并将其简化为黑白?