如何加载一个数据库。(100分)

  • 主题发起人 3DDELPHI
  • 开始时间
3

3DDELPHI

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
我想通过一个Opendialog加载一个*.DFB文件或是*.DB文件到DBGrid中该如何去做(请附源代码)。万分感激!!!
 
在TopenDialog设置好filter
比如设置为:
Alle Dateien (*.bw;*.cel;*.icb;*.pcc;*.pcd;*.pcx;*.pic;*.rgb;*.scr;*.tga;*.vda;*.vst;*.win;*.gif;*.tga;*.pcd;*.pcx;*.png;*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf)|*.bw;*.cel;*.icb;*.pcc;*.pcd;*.pcx;*.pic;*.rgb;*.scr;*.tga;*.vda;*.vst;*.win;*.gif;*.tga;*.pcd;*.pcx;*.png;*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf|SGI black/white images (*.bw)|*.bw|Autodesk images (*.cel)|*.cel|Truevision images (*.icb)|*.icb|ZSoft PCC images (*.pcc)|*.pcc|Kodak Photo-CD images (*.pcd)|*.pcd|ZSoft PCX images (*.pcx)|*.pcx|Autodesk images (*.pic)|*.pic|SGI true color images (*.rgb)|*.rgb|Word 5.x screen capture images (*.scr)|*.scr|Truevision images (*.tga)|*.tga|Truevision images (*.vda)|*.vda|Truevision images (*.vst)|*.vst|Truevision images (*.win)|*.win|GIF Image (*.gif)|*.gif|TGA-Format (*.tga)|*.tga|PCD-Format (*.pcd)|*.pcd|PCX-Format (*.pcx)|*.pcx|PNG-Format (*.png)|*.png|JPEG Bilddatei (*.jpg)|*.jpg|JPEG Bilddatei (*.jpeg)|*.jpeg|Bitmaps (*.bmp)|*.bmp|Symbole (*.ico)|*.ico|Erweiterte Metadateien (*.emf)|*.emf|Metadateien (*.wmf)|*.wmf22

再参照以下代码:
procedure TfrmColor.Button13Click(Sender: TObject);
Var
jpeg: TJPEGImage;
bmp: TBitmap;

FileExt: string[4];
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
FileExt := AnsiUpperCase(ExtractFileExt(OpenDialog1.Filename));
if (FileExt = '.BMP') or (FileExt = '.ICO') or (FileExt = '.WMF') or
(FileExt = '.EMF') or (FileExt = '.JPG') then
begin
Image1.Picture.LoadFromFile(OpenDialog1.Filename);
if (FileExt = '.BMP') then
begin
Caption := Caption +
Format(' (%d x %d)', [Image1.Picture.Width, Image1.Picture.Height]);
Image1.Picture := Image1.Picture;
end
else
if (FileExt = '.JPG') then
begin
Caption := Caption +
Format(' (%d x %d)', [Image1.Picture.Width, Image1.Picture.Height]);
Image1.Picture := Image1.Picture;
jpeg:= TJPEGImage.Create;
try
jpeg.LoadFromFile(OpenDialog1.Filename);
try
image1.Picture.bitmap.Assign( jpeg );
// image1.Picture.bmp.SaveTofile( ChangeFileExt( filename, '.BMP' ));
finally
end;
finally
jpeg.free
end;

end
else
if FileExt = '.ICO' then
begin
Icon := Image1.Picture.Icon;
Image1.Picture.Icon := Icon;
end;
if (FileExt = '.WMF') or (FileExt = '.EMF') then
Image1.Picture.Metafile := Image1.Picture.Metafile;
end;
end;
end;
 
楼上老兄贴错了吧?
to 3ddelphi:
不难啊!
开一个工程,在窗体上放一个TEdit,TTable,TDataSource,TDBGrid,TOpenDialog,TButton
将DataSource1.DataSet:=Table1,DBGrid1.DataSource:=DataSource,
Table1.Active:=False,Table1.DataBaseName:='',Table1.TableName:='';
设好OpenDialog的Filter属性,
响应Button1的Click事件

procedure TForm1.Button1Click(Sender: TObject);
var
s,t:String;
begin
if OpenDialog1.Execute then
begin
if Table1.Active then Table1.Close;
s:=OpenDialog1.FileName;
Edit1.Text:=s;
Table1.DatabaseName:=ExtractFileDir(s);//Paradox,DBase的库就是目录!
t:=ExtractFileExt(s);
if UpperCase(t)='.DB' then//根据文件的扩展名设数据库类型,如果多判断一下还可
//打开更多类型的表
Table1.TableType:=ttParadox
else Table1.TableType:=ttDBase;
Table1.TableName:=ExtractFileName(s);
try
Table1.Open;
except
ShowMessage('打开数据库表出错!');
end;
end;
end;
就行了!
 
二楼的兄弟,说得很清楚了,我就不多说了。
 
杜宝兄言之有理!
 
htw大侠说的太深了。杜宝大侠的好像不能成功,运行后出现以下异常:
Project opentable.exe raised exception class EDBEngineError with message 'corrupt table/index header! process stopped,use step or run to
conutinue.请指教是什么原因?多谢!!!
 
To:3DDELPHI
 
TO:3DDELPHI
你说的问题,我想可能是数据库驱动不对所导致。就是这:
...
if UpperCase(t)='.DB' then
Table1.TableType:=ttParadox
else Table1.TableType:=ttDBase;
...
我是根据文件的扩展名设数据库类型,把*.db当作都是Paradox文件,*.dbf都是Dbase文件,
但以*.db和*.dbf为扩展名的数据库还有多种,如:*.db可能是SyBase,*.dbf可能是VFP的表
我想你可能是用的VFP的表文件进行的试验,如果你用Delphi自带的DBDemos别名下的文件
测试就没有问题了。
这种错误可以通过多次判断数据库类型来解决。
 
我是用Database Desktop建立的Paradox文件,不知有什么问题?
DBDemos又是什么?多谢!!!
 
但是dBASE Ⅳ文件却能成功,真的搞不懂了
 
杜宝兄言之有理!
 
DBDemos好像是delphi自带的一个数据库。
 
多人接受答案了。
 

Similar threads

顶部