vvyang兄,我根据你提供的知识,编写了下面几个函数
红色的字体,就是我略有疑问的地方。
我的动机:当一个数据库已经建立好之后,只能在同一个库中新建不同的表,无法使用语句
DB := CreateOleObject('ADOX.Catalog');
那我使用语句
DBEngine := CreateOleObject('DAO.DBEngine.36');
DB := DBEngine.OpenDatabase(DBName);
来打开数据库并建立新表是否可行。耽误你的时间了,谢谢
unit DatabaseUnit;
interface
uses Windows, Messages, SysUtils, Variants, Classes,comobj,DB,ADODB,Controls;
//Access数据库中的表结构
type
TAccessTableDef = record
Name,
DateCreated,
LastUpdated,
Description: string;
end;
pTaccessTableDef = ^TAccessTableDef;
//Access数据库中的字段结构
type
TAccessFieldDef = record
Name: string;
Types,
Size: Longint;
Autoincrement: string;
Description: string;
end;
pTAccessFieldDef = ^TAccessFieldDef;
const
accessstring = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s';
adEmpty = $00000000; //空
adInteger = $00000003; //长整
adSingle = $00000004; //单精
adDouble = $00000005; //双精
adCurrency = $00000006; //货币
adVarWChar = $000000CA; //文本
adLongVarWChar = $000000CB; //备注
procedure CreateDatabase(DBName: string); //建立Access数据库
Procedure _CreateDatabaseTable(DBName: string; Tablenames:Tstrings; FieldDefsListArray: array of Tlist; arraycount: integer); //建立Access数据库中的表
procedure GetTableDefs(const DBName: string; TableDefsList: Tlist); //获得Access数据库中的表信息
procedure GetFieldDefs(const DBName, TableName: string; FieldDefsList: Tlist); //获得Access数据库中的每个表中的字段信息
procedure RenameField(const TableName, OldColName, NewColName: string; ADOConnection:TADOConnection ); //动态修改Access数据库字段内容
function CheckTablename(Const DBName, TableName: string): boolean;
implementation
procedure CreateDatabase(DBName: string); //建立access数据库
var
DB: OleVariant;
tempstr: string;
begin
DB := CreateOleObject('ADOX.Catalog');
try
tempstr := format(accessstring,[DBName]);
DB.Create(tempstr);
finally
DB:=Unassigned;
end;
end;
Procedure _CreateDatabaseTable(DBName: string; Tablenames:Tstrings; FieldDefsListArray: array of Tlist; arraycount: integer); //建立Access数据库中的表
var
DBEngine, DB,tables,Cols: OleVariant;
temploop1,temploop2: integer;
temppointer: pTAccessFieldDef;
begin
if not FileExists(DBName) then //如果没有发现数据库就建立数据库
CreateDatabase(DBName);
if TableNames.Count <= 0 then exit;
if TableNames.Count <> arraycount then exit; //名称和字段信息个数不一致,就退出
[red]DBEngine := CreateOleObject('DAO.DBEngine.36');
DB := DBEngine.OpenDatabase(DBName);[/red] try
for temploop1 := 0 to TableNames.Count -1 do
begin
if CheckTablename(DBName,TableNames[temploop1]) then //如果没有同名就建立
begin
tables := CreateOleObject('ADOX.Table');
tables.ParentCatalog := DBEngine;
tables.name := TableNames[temploop1]; //表建立好之后,就开始建立字段
if FieldDefsListArray[temploop1].Count > 0 then
begin
for temploop2 := 0 to FieldDefsListArray[temploop1].Count -1 do
begin
temppointer := FieldDefsListArray[temploop1].Items[temploop2];
Cols := CreateOleObject('ADOX.Column');
Cols.ParentCatalog := DBEngine;
case temppointer.Types of
adInteger: begin
Cols.Name := temppointer.Name;
Cols.type := temppointer.Types;
Cols.DefinedSize := temppointer.Size;
Cols.Properties['Autoincrement'].Value := strtobool(temppointer.Autoincrement);
Cols.Properties['Description'].Value := temppointer.Description;
tables.Columns.Append(Cols, adEmpty, 0);
Cols := Unassigned;
end;
adVarWChar: begin
Cols.Name := temppointer.Name;
Cols.type := temppointer.Types;
Cols.DefinedSize := temppointer.Size;
Cols.Properties['Description'].Value := temppointer.Description;
tables.Columns.Append(Cols, adEmpty, 0);
Cols := Unassigned;
end;
adLongVarWChar: begin
Cols.Name := temppointer.Name;
Cols.type := temppointer.Types;
Cols.DefinedSize := temppointer.Size;
Cols.Properties['Description'].Value := temppointer.Description;
tables.Columns.Append(Cols, adEmpty, 0);
Cols := Unassigned;
end;
end; //case temppointer.Types of
end; //for temploop2 := 0 to FieldDefsListArray[temploop1].Count -1 do
tables:= Unassigned; //每个表的所有字段都建立好之后,就注销,进入下一个表
Cols:= Unassigned;
end
else //if FieldDefsListArray[temploop1].Count > 0 then 如果字段小于等于0 就不要建立这个表的字段,
begin //进入下一张表
tables:= Unassigned;
Cols:= Unassigned;
end;
end //if CheckTablename(DBName,TableNames[temploop1]) then //如果没有同名就建立
end; //for temploop1 := 0 to TableNames.Count -1 do
finally
DBEngine:= Unassigned;
DB:= Unassigned;
tables:= Unassigned;
Cols:= Unassigned;
end;
end;
procedure GetTableDefs(const DBName: string; TableDefsList: Tlist); //获得Access数据库中的表信息
var
DBEngine, DB: OleVariant;
I: Longint;
temppointer: pTaccessTableDef;
begin
try
DBEngine := CreateOleObject('DAO.DBEngine.36');
DB := DBEngine.OpenDatabase(DBName);
if Longint(DB.TableDefs.Count) > 0 then
begin
for I := 0 to Longint(DB.TableDefs.Count)-1 do
begin
new(temppointer);
temppointer.Name := DB.TableDefs.Name;
temppointer.DateCreated := DB.TableDefs.DateCreated;
temppointer.LastUpdated := DB.TableDefs.LastUpdated;
try
temppointer.Description := DB.TableDefs.Properties['Description'].Value;
except
temppointer.Description := '';
end;
TableDefsList.Add(temppointer);
end; //for I := 0 to Longint(DB.TableDefs.Count)-1 do
end; // if Longint(DB.TableDefs.Count) > 0 then
finally
DB := Unassigned;
DBEngine := Unassigned;
end;
end;
procedure GetFieldDefs(const DBName, TableName: string; FieldDefsList: Tlist); //获得Access数据库中的每个表中的字段信息
var
DBEngine, DB: OleVariant;
I: Longint;
temppointer: pTAccessFieldDef;
begin
try
DBEngine := CreateOleObject('DAO.DBEngine.36');
DB := DBEngine.OpenDatabase(DBName);
if Longint(DB.TableDefs[TableName].Fields.Count) > 0 then
begin
for I := 0 to Longint(DB.TableDefs[TableName].Fields.Count) -1 do
begin
new(temppointer);
temppointer.Name := DB.TableDefs[TableName].Fields.Name;
temppointer.Types := DB.TableDefs[TableName].Fields.Type;
temppointer.Size := DB.TableDefs[TableName].Fields.Size;
try
temppointer.Description := DB.TableDefs[TableName].Fields.Properties['Description'].Value;
except
temppointer.Description := '';
end;
FieldDefsList.Add(temppointer);
end; //Longint(DB.TableDefs[TableName].Fields.Count) > 0
end; // Longint(DB.TableDefs[TableName].Fields.Count) > 0
finally
DB := Unassigned;
DBEngine := Unassigned;
end;
end;
//TableName: 表名; OldColName: 原字段名; NewColName: 新字段名;
procedure RenameField(const TableName, OldColName, NewColName: string; ADOConnection:TADOConnection );
var
DB, Col: OleVariant;
begin
try
DB := CreateOleObject('ADOX.Catalog');
DB.ActiveConnection := ADOConnection.ConnectionObject;
Col := CreateOleObject('ADOX.Column');
Col := DB.Tables[TableName].Columns[OldColName];
Col.Name := NewColName;
finally
DB := Unassigned;
Col := Unassigned;
end;
end;
function CheckTablename(Const DBName, TableName: string): boolean; //true 表示没有检测到相同表名,就可以建立,
var //false 表示检测到相同表名,不可以建立
tableNamelist: Tlist;
temploop: integer;
temppointer: pTaccessTableDef;
begin
result := false;
if not FileExists(DBName) then
CreateDatabase(DBName);
tableNamelist := Tlist.Create;
try
GetTableDefs(DBName,tableNameList);
if tableNamelist.Count > 0 then
begin
for temploop := 0 to TableNameList.Count -1 do
begin
temppointer := TableNamelist.Items[temploop];
if UpperCase(tableName) = UpperCase(temppointer.Name) then
exit;
end;
end;
result := true; //如果里面没有表也可以返回true
finally
tableNamelist.Clear;
tablenamelist.Free;
end;
end;
end.