2013-03-07 50 views
1

我是新來的Visual Basic,但我需要遍歷數據表中的行,並在測試腳本中使用的值,該腳本如下 -循環通過行動

Public Function TestMain(ByVal args() As Object) As Object 
    StartApp(URL) 

    ' HTML Browser ' 
    Browser_HtmlBrowser(Document_HomePage(),DEFAULT_FLAGS).Maximize() 

    Button_AddNewProfilesubmit().Click() 

    'here is where the rows would be read and the loop would start' 

    Text_Ctl00MainContentProfileNa().Click(AtPoint(6, 13)) 
    Browser_HtmlBrowser(Document_Http1921685526UserCon(), DEFAULT_FLAGS).InputChars("dataBase_Row_Value") 
    Table_HtmlTable_1().Click(AtCell(_ 
            AtRow(AtIndex(0)), _ 
            AtColumn(AtIndex(1)))) 

    'here is where the loop would end after all rows had been read' 

    Return Nothing 
End Function 

我有一個想法來實現這一點,首先做一個數據庫連接,然後創建循環 -

Dim pName As String 
    Dim datas As DataSet 
    Dim datar As DataRow 
    Dim oledat As SqlDataAdapter 
    oledat = New SqlDataAdapter("SELECT COLUMN FROM DATABASE",ConnectionString) 
    oledat.Fill(datas) 
    For Each datar In datas.Tables(0).Rows 
     pName = datar.Item("PROFILENAME") 

     Text_Ctl00MainContentProfileNa().Click(AtPoint(6, 13)) 
     Browser_HtmlBrowser(Document_Http1921685526UserCon(), DEFAULT_FLAGS).InputChars(pName) 
     Table_HtmlTable_1().Click(AtCell(_ 
            AtRow(AtIndex(0)), _ 
            AtColumn(AtIndex(1)))) 
    Next 

然而,這是打破,即使有在Visual Studio中沒有錯誤,也僅僅是datas之前使用的警告它被分配價值。我哪裏錯了?

+0

當你說這是打破你的意思。運行時會出錯嗎?如果是的話,錯誤是什麼。 – 2013-03-07 11:13:07

+0

當調試它不運行時,它只是說有構建錯誤,我用C#編程,如果有錯誤,它會這樣說。我正在使用IBM的理性測試軟件來生成腳本。 – Ebikeneser 2013-03-07 11:14:33

+0

那麼,構建錯誤是什麼?你說你剛剛有一個警告 - 這不會阻止它編譯 – 2013-03-07 11:15:41

回答

1

我相信你在使用它之前必須初始化一個新的數據集。示例:

Dim ds As DataSet = New DataSet() 
Dim connection As OleDb.OleDbConnection 
Dim command As OleDb.OleDbCommand 
Dim adapter As New OleDb.OleDbDataAdapter 
Dim connString As String = "my Connection string stuff;" 

connection = New OleDb.OleDbConnection(connString) 
Try 
    'open the connection 
    If connection.State = ConnectionState.Open Then 
    Else 
      connection.Open() 
    End If 
    'fill each data table 
    command = New OleDb.OleDbCommand(selectOne, connection) 
    adapter.SelectCommand = command 
    adapter.Fill(ds, "someTableName") 
Catch ex As OleDb.OleDbException 
    'error, do something 
Finally 
    'close everything down 
    adapter.Dispose() 
    If (Not command Is Nothing) Then 
     command.Dispose() 
    End If 
    connection.Close() 
End Try 

本示例使用OLEDB,但應與您正在做的相媲美。一旦你填充它,你應該能夠遍歷表格。但是,首先檢查以確保首先創建了一個數據集:

If (ds IsNot Nothing) Then 
    'do for statement here 
End If 

如果這不起作用,請告訴我。