读取字段信息:这个和读取表名是一样,也是为了避免不必要的浪费时间,做成手动来读取。
ADO读取字段信息也可以使用,但是有的数据库也不一定支持。
像读取表名那样,很多数据库都有特定的sql语句,但是又存在返回的信息不统一的问题,特别是对字段类型的描述,不同数据库之间差异较大。
个人目前是读取表的数据,使用返回的字段信息:
Function GetFieldsInfo(sTableName As String, ret() As FieldInfo) As RetCode
On Error GoTo errHandle
Dim rst As ADODB.Recordset
Set rst = adoconn.Execute("select * from " & sTableName & " where 1=2", adOpenForwardOnly, adLockReadOnly)
ReDim ret(rst.Fields.Count - 1) As FieldInfo
Dim i As Long
For i = 0 To rst.Fields.Count - 1
ret(i).SName = rst.Fields(i).name
ret(i).IType = rst.Fields(i).Type
ret(i).SType = GetFieldStrType(ret(i).IType)
Next
GetFieldsInfo = RetCode.SuccRT
Exit Function
errHandle:
GetFieldsInfo = ERROR_NO
StrErr = Err.Description
End Function
Private Function GetFieldStrType(i As DataTypeEnum) As String
Select Case i
Case adArray
GetFieldStrType = "adArray"
Case adBigInt
GetFieldStrType = "adBigInt"
Case adBinary
GetFieldStrType = "adBinary"
Case adBoolean
GetFieldStrType = "adBoolean"
Case adBSTR
GetFieldStrType = "adBSTR_Char"
Case adChapter
GetFieldStrType = "adChapter"
Case adChar
GetFieldStrType = "adChar"
Case adCurrency
GetFieldStrType = "adCurrency"
Case adDate
GetFieldStrType = "adDate"
Case adDBDate
GetFieldStrType = "adDBDate"
Case adDBTime
GetFieldStrType = "adDBTime"
Case adDBTimeStamp
GetFieldStrType = "adDBTimeStamp"
Case adDecimal
GetFieldStrType = "adDecimal"
Case adDouble
GetFieldStrType = "adDouble"
Case adEmpty
GetFieldStrType = "adEmpty"
Case adError
GetFieldStrType = "adError"
Case adFileTime
GetFieldStrType = "adFileTime"
Case adGUID
GetFieldStrType = "adGUID"
Case adIDispatch
GetFieldStrType = "adIDispatch"
Case adInteger
GetFieldStrType = "adInteger"
Case adIUnknown
GetFieldStrType = "adIUnknown"
Case adLongVarBinary
GetFieldStrType = "adLongVarBinary"
Case adLongVarChar
GetFieldStrType = "adLongVarChar"
Case adLongVarWChar
GetFieldStrType = "adLongVarWChar"
Case adNumeric
GetFieldStrType = "adNumeric"
Case adPropVariant
GetFieldStrType = "adPropVariant"
Case adSingle
GetFieldStrType = "adSingle"
Case adSmallInt
GetFieldStrType = "adSmallInt"
Case adTinyInt
GetFieldStrType = "adTinyInt"
Case adUnsignedBigInt
GetFieldStrType = "adUnsignedBigInt"
Case adUnsignedInt
GetFieldStrType = "adUnsignedInt"
Case adUnsignedSmallInt
GetFieldStrType = "adUnsignedSmallInt"
Case adUnsignedTinyInt
GetFieldStrType = "adUnsignedTinyInt"
Case adUserDefined
GetFieldStrType = "adUserDefined"
Case adVarBinary
GetFieldStrType = "adVarBinary"
Case adVarChar
GetFieldStrType = "adVarChar"
Case adVariant
GetFieldStrType = "adVariant"
Case adVarNumeric
GetFieldStrType = "adVarNumeric"
Case adVarWChar
GetFieldStrType = "adVarWChar"
Case adWChar
GetFieldStrType = "adWChar"
Case Else
GetFieldStrType = "UnKnowDataType"
End Select
End Function
这样能够保证字段的数据类型统一,在后续的一些处理上比较方便。