2014-01-17 97 views
0

我已經在VB.net應用程序中開發了一個字導出函數,該函數返回列(service_name)中具有特定值的所有行。但是,當循環遍歷函數時,單詞中的單元格總是抓取第一個服務名稱,而不是循環遍歷每個服務名稱。我需要一種可以指向特定service_name的下一個值的方式。SQL獲取所有行值VB.net

enter image description here

enter image description here

我已經給我掠從價值和出口詞明知有相匹配的價值,但總是返回的第一個其中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

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) 
+0

謝謝你的工作:) 我也必須改變爲我作爲整數= 0到sqlrowcount -1 – user3013243

0

每次調用getdataapplication時,要傳遞的價值0recordnum參數。因此,它總是要輸出第一行的數據。裏面你For循環,你可能想使用i代替0,例如:

Dim tempdatastore = getdataapplication(i, 1) 

然而,這似乎是一個非常恐怖的設計。每次調用getdataapplication方法時,該方法在循環的每次迭代中都會發生多次,它都迫使垃圾回收器執行重新查詢數據庫的操作。它會使更有意義查詢數據庫一次,然後循環返回DataSet的內容。您懷疑您不需要撥打GC.Collect

+0

謝謝,我會努力找到一個更好的解決方案。 – user3013243