How to find list of all tables in Access Database matching certain format in Delphi

泪湿孤枕 提交于 2019-12-24 08:47:43

问题


I need to compute a list of table names in a given database (MDB format), which have certain format (for example which contains DateTime field "Date Applied"). How can I do it?

Note: I am using Delphi 7, ADO components, Microsoft JET 4.0 to connect to MDB type Database.


回答1:


A schema may suit:

   Set rs = cn.OpenSchema(adSchemaColumns, _
   Array(Empty, Empty, Empty, SelectFieldName))

Where adSchemaColumns = 4
rs is a recordset object
cn a connection object
SelectFieldName is the column name, "Date Applied" in this case.

The constraints for this schema are:

TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME 

Columns (fields) returned are:

TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
COLUMN_GUID
COLUMN_PROPID
ORDINAL_POSITION
COLUMN_HASDEFAULT
COLUMN_DEFAULT
COLUMN_FLAGS
IS_NULLABLE
DATA_TYPE
TYPE_GUID
CHARACTER_MAXIMUM_LENGTH
CHARACTER_OCTET_LENGTH
NUMERIC_PRECISION
NUMERIC_SCALE
DATETIME_PRECISION
CHARACTER_SET_CATALOG
CHARACTER_SET_SCHEMA
CHARACTER_SET_NAME
COLLATION_CATALOG
COLLATION_SCHEMA
COLLATION_NAME
DOMAIN_CATALOG
DOMAIN_SCHEMA
DOMAIN_NAME
DESCRIPTION 

-- [Obtaining Schema Information from a Database](http://msdn.microsoft.com/en-us/library/kcax58fh(VS.80).aspx)




回答2:


There are two methods available on the connection component which are useful for this kind of work. The first is:

procedure GetTableNames(List: TStrings; SystemTables: Boolean = False); 

which populates a TStrings descendant with a list of all of the tables available in the current database. The next method is:

procedure GetFieldNames(const TableName: string; List: TStrings);

which populates a list of all fields for a specific table. You then can create a simple routine to loop through all fields for all tables for the specific field you are looking for.




回答3:


Here's a vbscript/asp type solution. You can adapt this to Delphi

Const adSchemaTables = 20


Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open _
    "Provider = Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source = 'C:\Scripts\Test.mdb'" 

Set objRecordSet = objConnection.OpenSchema(adSchemaTables)

Do Until objRecordset.EOF
    Wscript.Echo "Table name: " & objRecordset.Fields.Item("TABLE_NAME")
    Wscript.Echo "Table type: " & objRecordset.Fields.Item("TABLE_TYPE")
    Wscript.Echo
    objRecordset.MoveNext
Loop


来源:https://stackoverflow.com/questions/1851883/how-to-find-list-of-all-tables-in-access-database-matching-certain-format-in-del

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!