2013-03-07 53 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 

如果这不起作用,请告诉我。