循环行动

时间:2013-03-07 10:48:23

标签: sql database vb.net

我是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的警告。我哪里错了?

1 个答案:

答案 0 :(得分: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

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