首先呼叫adodb.connection物件中的openSchema函數,這樣會得到一個Recordset,其中每個記錄對應著資料庫中的一張表,紀錄的每個欄位都包含了對應表的某方面資訊。其中TABLE_NAME欄位包含了對應表的名稱可以把本功能寫成一個函數,函數的處理過程描述如下:
首先呼叫adodb.connection物件中的openSchema函數,這樣會得到一個Recordset,其中每個記錄對應著資料庫中的一張表,紀錄的每個欄位都包含了對應表的某方面資訊。其中TABLE_NAME欄位包含了對應表的名稱
然後遍歷這個Recordset,如果目前紀錄的TABLE_NAME欄位的值和要尋找的表的名字一樣,證明要尋找的表存在。
函數如下圖所示:
複製代碼代碼如下:
function check_gived_DataTable_exist_or_not(connect_object,name_of_gived_DataTable)
Do_gived_DataTable_exist=false
Const adSchemaTables=20 '表示想要取得資料庫中表格(table)和檢視(view)的集合
set RecordSet_about_table_and_view_in_DataBase=connect_object.openSchema(adSchemaTables)
Do Until RecordSet_about_table_and_view_in_DataBase.EOF
if RecordSet_about_table_and_view_in_DataBase(TABLE_TYPE)=TABLE then
if RecordSet_about_table_and_view_in_DataBase(TABLE_NAME)= name_of_gived_DataTable then
Do_gived_DataTable_exist=true
exit do
end if
end if
RecordSet_about_table_and_view_in_DataBase.movenext
Loop
check_gived_DataTable_exist_or_not=Do_gived_DataTable_exist
end function
註:
connect_object.openSchema(adSchemaTables)這個函數執行後,會得到資料庫中表格(table)和視圖(view)的集合,這是一個ADODB.Recordset類型的資料。
if RecordSet_about_table_and_view_in_DataBase(TABLE_TYPE)=TABLE這句話把檢查範圍縮小為表格(table)。