我在VB.net应用程序中开发了一个单词导出函数,它返回列(service_name)中具有特定值的所有行。但是,当循环执行该函数时,单词中的单元格始终会抓取第一个服务名称,而不是循环遍历每个服务名称。我需要一种方法,我可以指向特定service_name的下一个值。
我已经给出了表格的预览,我正在从中获取值和导出一词,知道有3行与值匹配但总是返回第一行。
用于返回SQL数据的函数:
Function getdataapplication(ByVal recordnum As Integer, ByVal fieldnum As Integer)
ds.Reset()
GC.Collect()
Dim dbtable
dbtable = "application_portfolio"
Dim sql As MySqlCommand
sql = New MySqlCommand("Select * from application_portfolio where service_name = '" & Wordexport.tbservicename.Text & "' ", dbcon)
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
DataAdapter1.SelectCommand = sql
DataAdapter1.Fill(ds, dbtable)
dbcon.Close()
sql.Dispose()
Return ds.Tables(dbtable).rows(recordnum).Item(fieldnum - 1)
'Return reader
End Function
Public Sub Word_Click(sender As Object, e As EventArgs) Handles Word.Click
Dim sqlrowcount As Integer
sqlrowcount = CountRecords()
tbcount.Text = sqlrowcount
If application_portfolio.Checked = True Then
'oWord.NewDocument.Application.Equals(Nothing)
Dim oPara2application As Word.Paragraph
Dim oTableapplication As Word.Table
For i As Integer = 1 To sqlrowcount
j = j + 1
oTableapplication = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, j, 7)
oTableapplication.Range.ParagraphFormat.SpaceAfter = 6
oTableapplication.Rows(1).Range.Font.Bold = True
oTableapplication.Cell(1, 1).Range.Text = "ID"
oTableapplication.Cell(1, 2).Range.Text = "Service Name"
oTableapplication.Cell(1, 3).Range.Text = "Application Name"
oTableapplication.Cell(1, 4).Range.Text = "Application Type"
oTableapplication.Cell(1, 5).Range.Text = "Cost"
oTableapplication.Cell(1, 6).Range.Text = "Year Released"
oTableapplication.Cell(1, 7).Range.Text = "Version"
Dim tempdatastore = getdataapplication(0, 1)
ListBox1.Items.Add(tempdatastore)
oTableapplication.Cell(j, 1).Range.Text = functions.getdataapplication(0, 1)
oTableapplication.Cell(j, 2).Range.Text = functions.getdataapplication(0, 2)
oTableapplication.Cell(j, 3).Range.Text = functions.getdataapplication(0, 3)
oTableapplication.Cell(j, 4).Range.Text = functions.getdataapplication(0, 4)
oTableapplication.Cell(j, 5).Range.Text = functions.getdataapplication(0, 5)
oTableapplication.Cell(j, 6).Range.Text = functions.getdataapplication(0, 6)
oTableapplication.Cell(j, 7).Range.Text = functions.getdataapplication(0, 7)
oTableapplication.Rows.Item(1).Range.Font.Italic = True
Next
End If
End function
答案 0 :(得分:0)
每次拨打getdataapplication
时,都会传递0
参数的值recordnum
。因此,它总是从第一行输出数据。在For
循环中,您可能希望使用i
代替0
,例如:
Dim tempdatastore = getdataapplication(i, 1)
然而,这似乎是一个非常可怕的设计。每次调用getdataapplication
方法时,每次循环迭代都会多次调用,它会强制垃圾收集器执行 AND 重新查询数据库。它会使远更有意义地查询数据库一次,然后遍历返回的DataSet
的内容。你不应该打电话给GC.Collect
。
答案 1 :(得分:0)
发现您的错误问题。你继续在你的函数getdataapplication
中搜索第0行oTableapplication.Cell(j, 1).Range.Text = functions.getdataapplication(0, 1)
oTableapplication.Cell(j, 2).Range.Text = functions.getdataapplication(0, 2)
oTableapplication.Cell(j, 3).Range.Text = functions.getdataapplication(0, 3)
oTableapplication.Cell(j, 4).Range.Text = functions.getdataapplication(0, 4)
oTableapplication.Cell(j, 5).Range.Text = functions.getdataapplication(0, 5)
oTableapplication.Cell(j, 6).Range.Text = functions.getdataapplication(0, 6)
oTableapplication.Cell(j, 7).Range.Text = functions.getdataapplication(0, 7)
您需要做的事情如下
oTableapplication.Cell(j, 1).Range.Text = functions.getdataapplication(i, 1)
oTableapplication.Cell(j, 2).Range.Text = functions.getdataapplication(i, 2)
oTableapplication.Cell(j, 3).Range.Text = functions.getdataapplication(i, 3)
oTableapplication.Cell(j, 4).Range.Text = functions.getdataapplication(i, 4)
oTableapplication.Cell(j, 5).Range.Text = functions.getdataapplication(i, 5)
oTableapplication.Cell(j, 6).Range.Text = functions.getdataapplication(i, 6)
oTableapplication.Cell(j, 7).Range.Text = functions.getdataapplication(i, 7)