VB查询SQL表的列名
来源:百度知道 编辑:UC知道 时间:2024/06/30 20:16:26
在VB中
如何查询SQL数据库中某表的全部列名
请给出具体代码谢谢!
如何查询SQL数据库中某表的全部列名
请给出具体代码谢谢!
Public Function getColList() As String()
'三个常用的数据库对象
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset, cat As New ADOX.Catalog
'保存列名的数组
Dim strCol() As String, i As Integer
'连接字符串
Dim pstr As String
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;"
pstr = pstr & "Data Source=" & App.Path & "数据库名称"
conn.Open pstr '打开数据库
cat.ActiveConnection = conn 'cat 和conn 连起来
'表对象,adox里的
Dim tbl As ADOX.Table
For Each tbl In cat.Tables
ReDim Preserve strCol(i)
strCol(i) = tbl.Name
i = i + 1
Next
getDateList = strDate '将列名数组返回
End Function
---------------------------------
连接字符串要改下,我这个函数是用来连 access 的, SQL可能有点不同.
不过也是大同小异.
如果不明白,可以到我空间看看,收集了一些ADO和ADOX的资料
declare @objid int,@objname char(40)
set @objname = 'tablename