F
fstao
Unregistered / Unconfirmed
GUEST, unregistred user!
1、有一完整的程序,是用来示范把数据库的表导出到其它路径。
组件如下:
CBAlias是TComboBox,作用是BDE的别名。
Table1是TTable,是表。
CBTable是TComboBox,作用是选择表。
EditDirAlias是TEdit,作用是显示路径。
BitBtn1,作用是导出数据
EditFic是TEdit,作用是需要导出数据的新名称。
可以到http://delphi.nease.net/DELPHI/D32SAMPL/COPYSO30.ZIP下载,如果下载
COPYSO30.ZIP,运行时,在菜单“Copy”的“Tables with Index to .dbf”,就是
以下的功能了。
unit Unit1;
interface
uses
Windows, BDE,Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,LibCs, FileCtrl, ExtCtrls, Grids, Buttons,DB, DBTables, ComCtrls, Menus;
type
TCopia_Dbf = class(TForm)
EditDirAlias: TEdit;
Table1: TTable;
CBAlias: TComboBox;
CBTable: TComboBox;
EditFic: TEdit;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure CBTableChange(Sender: TObject);
procedure CBAliasChange(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
public
iCount,NTablas,k : Integer;
Dir,Alias_S,Table_S,Index_S,Directory_T,Table_T,sAlias,Path : String;
TableType_T : TTableType;
function CopyTableFull(Alias_S, Table_S, Table_T: String; var sReturn :String): Boolean;
procedure ChangeDataBaseName;
end;
var
Copia_Dbf: TCopia_Dbf;
implementation
{$R *.DFM}
procedure TCopia_Dbf.ChangeDataBaseName;
var
i : Integer;
s : String;
List : TStringList;
begin
try
Session.GetTableNames(sAlias,'',True,False,CbTable.Items);
CbTable.Items.Insert(0, '< All Tables >');
CbTable.ItemIndex := 0;
// To obtain the path of the alias
if sALias[Length(sAlias)] = '/' then begin
EditDirAlias.Text := sAlias;
Exit;
end;
List := TStringList.Create;
Session.GetAliasParams(sAlias, List);
if Pos('PATH',List[0]) > 0 // .db; .dbf
then EditDirAlias.Text := Copy(List[0],6,Length(List[0]) - 5) + '/'
else if Pos('DATABASE',List[0]) > 0 // other tables
then begin
s := Copy(List[0],15,Length(List[0]) - 14);
i := Length(s);
while i > 0 do begin
if s = '/' then begin
EditDirAlias.Text := Copy(s,1,i);
Break;
end;
Dec(i);
end;
end;
finally
Screen.Cursor := CrDefault;
List.Free
end;
EditDirAlias.Hint := EditDirAlias.Text;
end;
function TCopia_Dbf.CopyTableFull(Alias_S, Table_S, Table_T: String; var sReturn :String): Boolean;
var
pTable_S, pTable_T : array[0..80] of char;
ResultCopy : DBIResult;
begin
{# Table_T it must contain the full Path of D.O.S. of the file}
Result := False;
{# To convert of String to Pchar the Destination table}
StrPCopy(pTable_T, Table_T);
Alias_S:='GZGL';
{# Config and copy the table}
with Table1 do begin
try
Close;
DataBaseName := Alias_S;
TableName := Table_S;
TableType := ttDefault;
StrPCopy(pTable_S, TableName);
Open;
ResultCopy := DbiCopyTable(DBHandle, True, pTable_S, nil, pTable_T);
Result := not (ResultCopy <> DBIERR_NONE);
if Result then sReturn := 'Table - ' + Table_S + ' - it has been successfully copied'
else sReturn := 'Error produced upon copying table';
except
on E: EDBEngineError do sReturn := E.Message;
on E: Exception do sReturn := E.Message;
end;
end;
end;
procedure TCopia_Dbf.FormCreate(Sender: TObject);
begin
Screen.Cursor := CrHourGlass;
try
Session.GetAliasNames(CbAlias.Items);
CbAlias.ItemIndex := 0;
finally
Screen.Cursor := CrDefault;
end;
{To launch the procedures of data update}
CbAliasChange(nil);
end;
procedure TCopia_Dbf.CBTableChange(Sender: TObject);
begin
sAlias:='GZGL';
with Table1 do begin
DisableControls;
Close;
DatabaseName :=sAlias ;
TableName :='DaoChu_Data.dbf';
Open;
Close;
EnableControls;
end;
end;
procedure TCopia_Dbf.CBAliasChange(Sender: TObject);
begin
sAlias :='GZGL';
ChangeDataBaseName;
end;
procedure TCopia_Dbf.BitBtn1Click(Sender: TObject);
var
iCount : Integer;
begin
{To Copy complete Tables. dbf + mdx}
Path:='D:/';
if CbTable.ItemIndex > 0 then
begin
{Copy an table}
if not CopyTableFull(CbAlias.Items[CbAlias.ItemIndex], {Alias Source}
CbTable.Items[CbTable.ItemIndex], {Table Source}
Path + AllTrim(EditFic.Text),{Alias + Table Target}
sReturn) {Message of result}
then
MessageDlg('Unable to Copy ' + CbTable.Items[ CbTable.ItemIndex ] +
'. Motive: ' + '. ' + #10 + #10 + sReturn, mtError, [mbOK], 0);
end
else
ShowMessage('The destination Directory must be indicated.');
end;
end.
我有一个问题是这样的:运行时,一定要选择CBTable,比如选择“DaoChu_Data.dbf”时,在EditFic.text
填定“ABC”,则D盘出现“ABC.dbf”。如果我只要直接导出别名为“GZGL”和表“DaoChu_Data.dbf”,不要
选择CBTable,在EditFic.text填定“ABC”,D盘出现“ABC.dbf”,那该如何改上面的程序呢?
组件如下:
CBAlias是TComboBox,作用是BDE的别名。
Table1是TTable,是表。
CBTable是TComboBox,作用是选择表。
EditDirAlias是TEdit,作用是显示路径。
BitBtn1,作用是导出数据
EditFic是TEdit,作用是需要导出数据的新名称。
可以到http://delphi.nease.net/DELPHI/D32SAMPL/COPYSO30.ZIP下载,如果下载
COPYSO30.ZIP,运行时,在菜单“Copy”的“Tables with Index to .dbf”,就是
以下的功能了。
unit Unit1;
interface
uses
Windows, BDE,Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,LibCs, FileCtrl, ExtCtrls, Grids, Buttons,DB, DBTables, ComCtrls, Menus;
type
TCopia_Dbf = class(TForm)
EditDirAlias: TEdit;
Table1: TTable;
CBAlias: TComboBox;
CBTable: TComboBox;
EditFic: TEdit;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure CBTableChange(Sender: TObject);
procedure CBAliasChange(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
public
iCount,NTablas,k : Integer;
Dir,Alias_S,Table_S,Index_S,Directory_T,Table_T,sAlias,Path : String;
TableType_T : TTableType;
function CopyTableFull(Alias_S, Table_S, Table_T: String; var sReturn :String): Boolean;
procedure ChangeDataBaseName;
end;
var
Copia_Dbf: TCopia_Dbf;
implementation
{$R *.DFM}
procedure TCopia_Dbf.ChangeDataBaseName;
var
i : Integer;
s : String;
List : TStringList;
begin
try
Session.GetTableNames(sAlias,'',True,False,CbTable.Items);
CbTable.Items.Insert(0, '< All Tables >');
CbTable.ItemIndex := 0;
// To obtain the path of the alias
if sALias[Length(sAlias)] = '/' then begin
EditDirAlias.Text := sAlias;
Exit;
end;
List := TStringList.Create;
Session.GetAliasParams(sAlias, List);
if Pos('PATH',List[0]) > 0 // .db; .dbf
then EditDirAlias.Text := Copy(List[0],6,Length(List[0]) - 5) + '/'
else if Pos('DATABASE',List[0]) > 0 // other tables
then begin
s := Copy(List[0],15,Length(List[0]) - 14);
i := Length(s);
while i > 0 do begin
if s = '/' then begin
EditDirAlias.Text := Copy(s,1,i);
Break;
end;
Dec(i);
end;
end;
finally
Screen.Cursor := CrDefault;
List.Free
end;
EditDirAlias.Hint := EditDirAlias.Text;
end;
function TCopia_Dbf.CopyTableFull(Alias_S, Table_S, Table_T: String; var sReturn :String): Boolean;
var
pTable_S, pTable_T : array[0..80] of char;
ResultCopy : DBIResult;
begin
{# Table_T it must contain the full Path of D.O.S. of the file}
Result := False;
{# To convert of String to Pchar the Destination table}
StrPCopy(pTable_T, Table_T);
Alias_S:='GZGL';
{# Config and copy the table}
with Table1 do begin
try
Close;
DataBaseName := Alias_S;
TableName := Table_S;
TableType := ttDefault;
StrPCopy(pTable_S, TableName);
Open;
ResultCopy := DbiCopyTable(DBHandle, True, pTable_S, nil, pTable_T);
Result := not (ResultCopy <> DBIERR_NONE);
if Result then sReturn := 'Table - ' + Table_S + ' - it has been successfully copied'
else sReturn := 'Error produced upon copying table';
except
on E: EDBEngineError do sReturn := E.Message;
on E: Exception do sReturn := E.Message;
end;
end;
end;
procedure TCopia_Dbf.FormCreate(Sender: TObject);
begin
Screen.Cursor := CrHourGlass;
try
Session.GetAliasNames(CbAlias.Items);
CbAlias.ItemIndex := 0;
finally
Screen.Cursor := CrDefault;
end;
{To launch the procedures of data update}
CbAliasChange(nil);
end;
procedure TCopia_Dbf.CBTableChange(Sender: TObject);
begin
sAlias:='GZGL';
with Table1 do begin
DisableControls;
Close;
DatabaseName :=sAlias ;
TableName :='DaoChu_Data.dbf';
Open;
Close;
EnableControls;
end;
end;
procedure TCopia_Dbf.CBAliasChange(Sender: TObject);
begin
sAlias :='GZGL';
ChangeDataBaseName;
end;
procedure TCopia_Dbf.BitBtn1Click(Sender: TObject);
var
iCount : Integer;
begin
{To Copy complete Tables. dbf + mdx}
Path:='D:/';
if CbTable.ItemIndex > 0 then
begin
{Copy an table}
if not CopyTableFull(CbAlias.Items[CbAlias.ItemIndex], {Alias Source}
CbTable.Items[CbTable.ItemIndex], {Table Source}
Path + AllTrim(EditFic.Text),{Alias + Table Target}
sReturn) {Message of result}
then
MessageDlg('Unable to Copy ' + CbTable.Items[ CbTable.ItemIndex ] +
'. Motive: ' + '. ' + #10 + #10 + sReturn, mtError, [mbOK], 0);
end
else
ShowMessage('The destination Directory must be indicated.');
end;
end.
我有一个问题是这样的:运行时,一定要选择CBTable,比如选择“DaoChu_Data.dbf”时,在EditFic.text
填定“ABC”,则D盘出现“ABC.dbf”。如果我只要直接导出别名为“GZGL”和表“DaoChu_Data.dbf”,不要
选择CBTable,在EditFic.text填定“ABC”,D盘出现“ABC.dbf”,那该如何改上面的程序呢?