在VB中显示Listbox中的数据

时间:2012-03-28 10:08:36

标签: vb.net

我想在Listbox中显示来自数据库的数据...这是我的代码,它不起作用。在列表框中,它显示为对象[,]。数组.....我能做什么......

Dim DBCONSRT,QRYSTR As String

    Dim strSQL As String

    Dim DBCON, myConn, myCommand, rs As Object

    Dim NoOfRecords As Long

    rs = CreateObject("ADODB.Recordset")

    Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=user;PASSWORD=pass;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Initial Catalog=QA1MM;"

    myConn = CreateObject("ADODB.Connection")
    myCommand = CreateObject("ADODB.Command")
    myConn.Open(DB_CONNECT_STRING)
    myCommand.ActiveConnection = myConn
    strSQL = "select * from QA1MM.STRSK_OH with ur;"
    rs.Open(strSQL, myConn)
    ListBox1.Items.Add(rs.GetRows(2))
    myConn.Close()

1 个答案:

答案 0 :(得分:0)

GetRows方法用于从Recordset中检索指定数量的行,并使用结果数据填充数组。所以解决方案是首先将项添加到数组中,然后迭代以将数据添加到列表框中。

rs.Open(strSQL, myConn)
    While Not rs.EOF

        ListBox1.Items.Add(rs(0).value)

        rs.MoveNext()
    End While
myConn.Close()

对于datagrid:

Set DataGrid1.DataSource = rs
DataGrid1.Refresh