*******请教:如何用程序实现简单的数据库的建立???(200分!!!!)(100分)

  • 主题发起人 主题发起人 happyfox
  • 开始时间 开始时间
H

happyfox

Unregistered / Unconfirmed
GUEST, unregistred user!
在做delphi 应用软件过程中,首先要定义数据库(如:access)。在access中建立数据表的字段名,字段类型,长度等,然后在delphi 中引用该数据库!.
我想在delphi 程序中生成一个象ACCESS建数据表时的界面,实现动态自动定义一个程序要用的数据库。
该如何写这种程序????
谢谢.....
 
这个给你参考。
procedure CreateNewDatabase(DatabaseFileName: string);
var
cat: OleVariant;
begin
if FileExists(DatabaseFileName) then
begin
if MessageBox(Application.Handle, PChar('Database ' + DatabaseFileName + ' has existed!' + #13#10
+ 'Delete this database and create a new database ?'), 'Database Exists',
MB_YESNO + MB_ICONWARNING + MB_DEFBUTTON2) = mrNo then
exit;
if not DeleteFile(DatabaseFileName) then
begin
MessageBox(Application.Handle,
PChar('Cannot delete database ' + DatabaseFileName),
'Delete Database Error!', MB_OK + MB_ICONERROR);
exit;
end;
end;
cat := CreateOleObject('ADOX.Catalog');
cat.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DatabaseFileName);
if ConnectDatabase(DatabaseFileName) then
begin
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Table FaqClass ' +
'(' +
'ClassID INT not null,' +
'ClassTitle char(100) not null,' +
'ParentClassID INT not null,' +
'ClassType TINYINT DEFAULT 0' +
')';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create UNIQUE Index ClassIDIndex ON FaqClass (ClassID)';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Index ClassTitleIndex ON FaqClass (ClassTitle)';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Index ParentClassIDIndex ON FaqClass (ParentClassID)';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Index ClassTypeIndex ON FaqClass (ClassType)';
adoquryClass.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Table FaqMemo' +
'(' +
'MemoID INT not null,' +
'ParentClassID INT not null,' +
'MemoTitle char(100) not null,' +
'MemoText TEXT,' +
'MemoPublic BIT DEFAULT 0,' + //缺省是不公开
'MemoLastModifyTime DATETIME' +
')';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create UNIQUE Index MemoIDIndex ON FaqMemo (MemoID)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index ParentClassIDIndex ON FaqMemo (ParentClassID)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index MemoTitleIndex ON FaqMemo (MemoTitle)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index MemoPublicIndex ON FaqMemo (MemoPublic)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index MemoLastModifyTimeIndex ON FaqMemo (MemoLastModifyTime)';
adoquryMemo.ExecSQL;
end;
end;
 
我需要的是象access(或desktop) 建数据库表的 form!!! 在form中实现建立数据库表?????
[:D]
 
我的这个就是其核心!
至于具体的那些仅仅是界面的实现罢了。
 
有没有现成的组件?
 
我做了个小工具www.easyrp.com
 
这是我的程序:
uses comobj, ADODB2000, DB, ADODB;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
CreateAccess: OleVariant;
tbl: OleVariant;
MyConnection, MyAccess, MyTable: OleVariant;
MyAccessName, MyTableName: string;
SearchRec: TSearchRec;
TableFlag: Boolean;
i: integer;
begin
MyAccess := CreateOleObject('ADOX.Catalog');
MyTable := CreateOleObject('ADOX.Table');

MyAccessName := ExtractFilePath(Application.ExeName) + 'Test.MDB';
if FindFirst(MyAccessName, faAnyFile, SearchRec) = 0 then
begin
MyAccess.ActiveConnection := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + MyAccessName;
end
else
begin
MyAccess.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + MyAccessName);
end;
//判断库中是否有该天的表。没有的话添加一个

MyTableName := 'MyTable';
TableFlag := False;
if MyAccess.Tables.count > 0 then
begin
for i := 0 to MyAccess.Tables.count-1 do
begin
if MyAccess.Tables.item.name = MyTableName then TableFlag := True;
end;
end;


if not TableFlag then
begin
//添加表
MyTable.name := MyTableName;
MyTable.Columns.Append('Column1', adInteger);
MyTable.Columns.Append('Column2', adDate);

MyTable.Columns.Append('Column6', adVarWChar, 50);
MyAccess.Tables.Append(MyTable);
end
else
MyTable.name := MyTableName;

ADODataSet1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+MyAccessName;
end;

 
后退
顶部