如何判断Access数据库的表中的字段是否存在索引(300)

  • 主题发起人 主题发起人 cocw
  • 开始时间 开始时间
C

cocw

Unregistered / Unconfirmed
GUEST, unregistred user!
用Delphi操作,如何判断Access数据库的表中的字段是否存在索引?
 
1、{* 获得数据库表名称 *}procedure GetTableName(ATables: TStrings; Connection: TADOConnection);varTypeField,NameField: TField;TableType: string;DataSet: TADODataSet;beginDataSet := TADODataSet.Create(nil);try Connection.OpenSchema(siTables, EmptyParam, EmptyParam, DataSet); TypeField := DataSet.FieldByName('TABLE_TYPE'); {* do not localize *} NameField := DataSet.FieldByName('TABLE_NAME'); {* do not localize *} ATables.BeginUpdate; try ATables.Clear; while not DataSet.EOF do begin TableType := TypeField.AsString; if (TableType = 'TABLE') then ATables.Add(NameField.AsString); DataSet.Next; end; finally ATables.EndUpdate; end;finally DataSet.Free;end;end;2、{* 获得数据库系统表名称 *}procedure GetSysTableName(ATables: TStrings; Connection: TADOConnection);varTypeField,NameField: TField;TableType: string;DataSet: TADODataSet;beginDataSet := TADODataSet.Create(nil);try Connection.OpenSchema(siTables, EmptyParam, EmptyParam, DataSet); TypeField := DataSet.FieldByName('TABLE_TYPE'); {* do not localize *} NameField := DataSet.FieldByName('TABLE_NAME'); {* do not localize *} ATables.BeginUpdate; try ATables.Clear; while not DataSet.EOF do begin TableType := TypeField.AsString; if (TableType = 'SYSTEM TABLE') then ATables.Add(NameField.AsString); DataSet.Next; end; finally ATables.EndUpdate; end;finally DataSet.Free;end;end;3、{* 获得数据库视图名称 *}procedure GetViewName(AViews: TStrings; Connection: TADOConnection);varTypeField,NameField: TField;TableType: string;DataSet: TADODataSet;beginDataSet := TADODataSet.Create(nil);try Connection.OpenSchema(siTables, EmptyParam, EmptyParam, DataSet); TypeField := DataSet.FieldByName('TABLE_TYPE'); {* do not localize *} NameField := DataSet.FieldByName('TABLE_NAME'); {* do not localize *} AViews.BeginUpdate; try AViews.Clear; while not DataSet.EOF do begin TableType := TypeField.AsString; if (TableType = 'VIEW') then AViews.Add(NameField.AsString); DataSet.Next; end; finally AViews.EndUpdate; end;finally DataSet.Free;end;end;4、{* 获得表主键 *}procedure GetPrimaryKeyField(AKeys: TStrings; const ATable: string; Connection: TADOConnection);varADODataSet: TADODataSet;ARestrictions: OleVariant;Field: TField;beginADODataSet := TADODataSet.Create(nil);try ARestrictions := VarArrayOf([Null, Null, ATable]); Connection.OpenSchema(siPrimaryKeys, ARestrictions, EmptyParam, ADODataSet); Field := ADODataSet.FindField('COLUMN_NAME'); ADODataSet.First; while not ADODataSet.Eof do begin AKeys.Add(Field.AsString); ADODataSet.Next; end;finally ADODataSet.Free;end;end;5、{* 获得表外键名称 *}procedure GetForeignKeyName(AKeys: TStrings; const ATable: string; Connection: TADOConnection);varADODataSet: TADODataSet;FieldTable, FieldColumn: TField;beginADODataSet := TADODataSet.Create(nil);try Connection.OpenSchema(siForeignKeys, EmptyParam, EmptyParam, ADODataSet); FieldTable := ADODataSet.FindField('FK_TABLE_NAME'); FieldColumn := ADODataSet.FindField('FK_NAME'); ADODataSet.First; while not ADODataSet.Eof do begin if SameText(FieldTable.AsString, ATable) then begin AKeys.Add(FieldColumn.AsString); end; ADODataSet.Next; end;finally ADODataSet.Free;end;end;6、{* 获得表索引名称 *}procedure GetIndexName(AIndexs: TStrings; const ATable: string; Connection: TADOConnection);varADODataSet: TADODataSet;FieldTable, FieldIndex, FieldColumn, FieldPrimary, FieldUnique: TField;sText: string;beginADODataSet := TADODataSet.Create(nil);try Connection.OpenSchema(siIndexes, EmptyParam, EmptyParam, ADODataSet); FieldTable := ADODataSet.FindField('TABLE_NAME'); FieldIndex := ADODataSet.FindField('INDEX_NAME'); FieldColumn := ADODataSet.FindField('COLUMN_NAME'); FieldPrimary := ADODataSet.FindField('PRIMARY_KEY'); FieldUnique := ADODataSet.FindField('UNIQUE'); ADODataSet.First; while not ADODataSet.Eof do begin if SameText(FieldTable.AsString, ATable) then begin if FieldPrimary.AsBoolean then sText := 'PrimaryKey' else if FieldUnique.AsBoolean then sText := 'Unique' else sText := ''; if sText <> '' then sText := Format('%s (%s, %s)', [FieldIndex.AsString, FieldColumn.AsString, sText]) else sText := Format('%s (%s)', [FieldIndex.AsString, FieldColumn.AsString]); AIndexs.Add(sText); end; ADODataSet.Next; end;finally ADODataSet.Free;end;end;7、{* 获得视图脚本 *}function GetViewScript(AView: string; Connection: TADOConnection): string;varADODataSet: TADODataSet;FieldView, FieldScript: TField;beginADODataSet := TADODataSet.Create(nil);try Connection.OpenSchema(siViews, EmptyParam, EmptyParam, ADODataSet); ADODataSet.First; FieldView := ADODataSet.FieldByName('TABLE_NAME'); FieldScript := ADODataSet.FieldByName('VIEW_DEFINITION'); while not ADODataSet.Eof do begin if SameText(FieldView.AsString, AView) then begin Result := FieldScript.AsString; Break; end; ADODataSet.Next; end;finally ADODataSet.Free;end;end;8、{* 获得表字段信息 *}procedure GetTableStruct(ATable: string);varADODataSet: TADODataSet;ARestrictions: OleVariant;beginADODataSet := TADODataSet.Create(nil);ARestrictions := VarArrayOf([Null, Null, tvDB.Selected.Text]);conDB.OpenSchema(siColumns, ARestrictions, EmptyParam, ADODataSet);end;
 
后退
顶部