'用ado怎样实现 '工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号) '---------------------------------------------------------------------------- Private Sub Form_Load() If ExistsTable("MyTable") Then MsgBox "存在表MyTable" End If End SubPrivate Function ExistsTable(ByVal strTableName As String) As Boolean ExistsTable = False Dim adoCN As New ADODB.Connection '定义数据库的连接 Dim strCnn As String Dim rstSchema As New ADODB.Recordset strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False" adoCN.Open strCnn
Set rstSchema = adoCN.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF If rstSchema!TABLE_TYPE = "TABLE" Then If UCase(rstSchema!TABLE_NAME) = UCase(strTableName) Then ExistsTable = True Exit Do End If End If rstSchema.MoveNext Loop rstSchema.Close adoCN.Close Set rstSchema = Nothing Set adoCN = Nothing
rst.open "select * from sysobjects where name='TblName'" ,cnn if rst.recordcount=0 then msgbox "不存在此表" else msgbox "存在" end if
rst.open "select * from sysobjects where name='TblName'" ,cnn if rst.recordcount=0 then msgbox "不存在此表" else msgbox "存在" end if
引用Microsoft ADO Extensions 2.1 for DDL and Security (ADOX)): (需要升级至VB 6.0SP3) Dim adoConnectionX As New ADODB.Connection 'Ms SQL 7: adoConnectionX.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Data Source=yuer;DataBase=NorthwindCS" 'Access 2000: 'adoConnectionX.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\DRptPlus\DRptPlus\Data\NWind2K.mdb;Persist Security Info=False"
Dim adoxCatalogX As New ADOX.Catalog Set adoxCatalogX.ActiveConnection = adoConnectionX
Dim TestTableName As String TestTableName = "产品"
Dim adoxTableX As ADOX.Table For Each adoxTableX In adoxCatalogX.Tables If adoxTableX.Name = TestTableName Then MsgBox "[" & TestTableName & "]表已存在!" Exit For End If Next
'用ado怎样实现 '工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号) '---------------------------------------------------------------------------- Private Sub Form_Load() MsgBox "表MyTable中有 " & fieldCount & " 个字段" End Sub Private Function fieldCount(ByVal strTableName As String) As Integer fieldCount = 0 Dim adoCN As New ADODB.Connection '定义数据库的连接 Dim strCnn As String Dim rstSchema As New ADODB.Recordset strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False" adoCN.CursorLocation = adUseClient adoCN.Open strCnn '查询MyTable,得到字段数 rstSchema.Open "select top 1 * from MyTable", adoCN, adOpenDynamic, adLockOptimistic fieldCount = rstSchema.Fields.Count rstSchema.Close adoCN.Close Set rstSchema = Nothing Set adoCN = Nothing End Function
'工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
'----------------------------------------------------------------------------
Private Sub Form_Load()
If ExistsTable("MyTable") Then
MsgBox "存在表MyTable"
End If
End SubPrivate Function ExistsTable(ByVal strTableName As String) As Boolean
ExistsTable = False
Dim adoCN As New ADODB.Connection '定义数据库的连接
Dim strCnn As String
Dim rstSchema As New ADODB.Recordset strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False"
adoCN.Open strCnn
Set rstSchema = adoCN.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
If rstSchema!TABLE_TYPE = "TABLE" Then
If UCase(rstSchema!TABLE_NAME) = UCase(strTableName) Then
ExistsTable = True
Exit Do
End If
End If
rstSchema.MoveNext
Loop
rstSchema.Close
adoCN.Close
Set rstSchema = Nothing
Set adoCN = Nothing
End Function
-------------------------------------------------------------------------
上面的连接多写了一个.mdb,改一下:strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb;Persist Security Info=False"
if rst.recordcount=0 then
msgbox "不存在此表"
else
msgbox "存在"
end if
if rst.recordcount=0 then
msgbox "不存在此表"
else
msgbox "存在"
end if
(需要升级至VB 6.0SP3)
Dim adoConnectionX As New ADODB.Connection
'Ms SQL 7:
adoConnectionX.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Data Source=yuer;DataBase=NorthwindCS"
'Access 2000:
'adoConnectionX.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\DRptPlus\DRptPlus\Data\NWind2K.mdb;Persist Security Info=False"
Dim adoxCatalogX As New ADOX.Catalog
Set adoxCatalogX.ActiveConnection = adoConnectionX
Dim TestTableName As String
TestTableName = "产品"
Dim adoxTableX As ADOX.Table
For Each adoxTableX In adoxCatalogX.Tables
If adoxTableX.Name = TestTableName Then
MsgBox "[" & TestTableName & "]表已存在!"
Exit For
End If
Next
http://soft.mrol.net/article/56/57/2006/2006013111939.html 你读懂这些代码就很容易了,其实代码也很容易的,呵呵
抢分的还真快呀呵呵
'工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
'----------------------------------------------------------------------------
Private Sub Form_Load()
MsgBox "表MyTable中有 " & fieldCount & " 个字段"
End Sub
Private Function fieldCount(ByVal strTableName As String) As Integer
fieldCount = 0
Dim adoCN As New ADODB.Connection '定义数据库的连接
Dim strCnn As String
Dim rstSchema As New ADODB.Recordset strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False"
adoCN.CursorLocation = adUseClient
adoCN.Open strCnn
'查询MyTable,得到字段数
rstSchema.Open "select top 1 * from MyTable", adoCN, adOpenDynamic, adLockOptimistic
fieldCount = rstSchema.Fields.Count
rstSchema.Close
adoCN.Close
Set rstSchema = Nothing
Set adoCN = Nothing
End Function