SQL使整行依赖于第一列

时间:2013-08-15 09:52:37

标签: sql vb6

我正在制作一个VB6程序,我想从数据库中获取信息。

但我只需要表格中一行的信息

目前我将此作为我的查询

SELECT CertNo FROM STICKER

这会在列表框中填入CertNo中的所有项目, 有没有办法让信息依赖于变量,比如

SELECT CertNo FROM STICKER WHERE CertNo = 10000

它将从CertNo = 10000

的行中获取所有列

获取数据的代码:

Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long

Datalist.Clear

Set cn = New ADODB.Connection

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                              "Data Source=C:\Users\User\Desktop\test\dbAdd.mdb;"

Set rs = New ADODB.Recordset

rs.Open "SELECT CertNo FROM STICKER WHERE CertNo = '10303';", cn, adOpenStatic, adLockOptimistic

rs.MoveFirst

While Not rs.EOF
    Datalist.AddItem rs.Fields(0)
    rs.MoveNext
Wend

Set rs = Nothing
Set cn = Nothing

1 个答案:

答案 0 :(得分:2)

如果您需要获取Data1

等字段的结果
rs.Open "SELECT CertNo FROM STICKER WHERE CertNo = '"& Data1 &"';", cn, adOpenStatic, adLockOptimistic

如果你想从sql返回多个列,请执行以下操作

rs.Open "SELECT CertNo, IssueDate FROM STICKER WHERE CertNo = '"& Data1 &"';", cn, adOpenStatic, adLockOptimistic

并在循环中

While Not rs.EOF
    Datalist.AddItem rs.Fields(0) & rs.Fields(1)
    rs.MoveNext
Wend