SQL获取值为VB.net的所有行
我在VB.net应用程序中开发了一个word导出函数,它返回列service_name中具有特定值的所有行。然而,当在函数中循环时,word中的单元格总是获取第一个服务名称,而不是循环遍历每个服务名称。我需要一种方法,可以指向特定服务名称的下一个值 我已经给出了我从中获取值的表和单词export的预览,知道有3行与值匹配,但总是返回第一行 用于返回SQL数据的函数:SQL获取值为VB.net的所有行,sql,vb.net,cursor,office-interop,Sql,Vb.net,Cursor,Office Interop,我在VB.net应用程序中开发了一个word导出函数,它返回列service_name中具有特定值的所有行。然而,当在函数中循环时,word中的单元格总是获取第一个服务名称,而不是循环遍历每个服务名称。我需要一种方法,可以指向特定服务名称的下一个值 我已经给出了我从中获取值的表和单词export的预览,知道有3行与值匹配,但总是返回第一行 用于返回SQL数据的函数: Function getdataapplication(ByVal recordnum As Integer, ByVal fie
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
每次调用getdataapplication时,都会传递recordnum参数的值0。因此,它总是从第一行输出数据。在For循环中,您可能希望使用i而不是0,例如:
Dim tempdatastore = getdataapplication(i, 1)
然而,这似乎是一个非常糟糕的设计。每次调用getdataapplication方法(循环每次迭代都会发生多次)时,它都会强制垃圾收集器执行其操作,并重新查询数据库。查询数据库一次,然后遍历返回的数据集的内容,这将更有意义。您是否不需要调用GC.Collect是值得怀疑的。发现了您的错误问题。在函数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)
谢谢,它成功了:我还必须将I As Integer=0改为sqlrowcount-1谢谢,我会尝试找到更好的解决方案。