其实判断数据库表及其字段的方式很多,如ADOX对象。不过,当你的系统中存在了ADODB对象时,根本没有必要再去使用其它对象,减少发布的麻烦。这里便是利用Connection和Recordset对象,在VB6中判断数据库中是否存在某表或某字段。
以下是通过该方式写的两个通用函数。
' -------------------------------------------------------
' 判断数据库表中是否存在某个数据库表
'
' 参数:
' strConn:连接字符串;TableName:表名称
' 返回值:
' 若存在返回True;否则返回False
' -------------------------------------------------------
Function ChkTable(Byval strConn As String,Byval TableName As String) As Boolean
Dim tbs As ADODB.Recordset
Dim conn as new ADODB.Connection
conn..ConnectionString=strConn
conn.Open
Set tbs=CONN.OpenSchema(20)
tbs.Filter="TABLE_NAME='"&TableName&"' "
If Not tbs.eof then
chktable=true
Else chktable=false
End if
conn.Close
set conn=Nothing
tbs.Close
set tbs=nothing
End function
' --------------------------------------------------------------------------------------
' 判断数据库表中是否存在某个字段
'
' 参数:
' strConn:连接字符串;TableName:表名称,ColumnName 字段名
' 返回值:
' 若存在返回True;否则返回False
' -------------------------------------------------------------------------------------
'
Function ChkColumn(Byval strConn As String,ByVal TabName As String, ByVal ColumnName As String) As Boolean
Dim rs As ADODB.Recordset
Dim conn as new ADODB.Connection
conn..ConnectionString=strConn
conn.Open
Set rs = Conn.OpenSchema(4)
rs.Filter = "COLUMN_NAME='" & ColumnName & "' And " & "TABLE_NAME='" & TabName & "' "
If Not rs.EOF Then
ChkColumn = True
Else
ChkColumn = False
End If
conn.Close
set conn=Nothing
rs.Close
Set rs = Nothing
End Function