谁能提供DBF和ACESS数据库文件驱动程序的使用接口(200分)

  • 主题发起人 主题发起人 yahe
  • 开始时间 开始时间
Y

yahe

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能提供DBF和ACESS数据库文件驱动程序的使用接口,
我希望不用BDE直接调用它们,并对其进行操作,
英文或中文都行!
最好做一些解释!
 
ACESS数据库文件驱动程序:参见delphi光盘中的Msda_type.exe中的说明。
 
我怎么没有找到呢?
 
这个是我做的直接操作Dbf格式的类,你自己看看,有问题再来信!
unit guangfadbf;
interface
uses Windows, SysUtils, Classes;
type
TdbfHeader = record { Dbf文件头结构 }
VersionNumber :byte; { 版本号(无.FBT文件为03H,有为83H) }
LastUpdateYear :byte; { 最后修改的年份 }
LastUpdateMonth :byte; { 最后修改的月份 }
LastUpdateDay :byte; { 最后修改的天份 }
NumberOfRecords :longint; { 记录数 }
BytesInHeader :smallint; { 数据库文件头数据结构占用的长度 }
BytesInRecords :smallint; { 记录长度 }
ReservedInHeader :array[0..19] of char;{ 保留 }
end;
type
TdbfField = record { 字段结构 }
FieldName :array[0..10] of char; { 字段名 }
FieldType :char; { ASCII码表示的字段类型(C,N,L,D,M) }
fld_addr :longint; { 数据地址 }
Width :byte; { 字段长度 }
Decimals :byte; { 小数位数 }
Reserved :array[0..13] of byte; { 保留 }
end;
type
TTempField = record {临时字段记录变量}
FieldName :array[0..10] of char; { 字段名 }
FieldType :char; { ASCII码表示的字段类型(C,N,L,D,M) }
fld_addr :longint; { 数据地址 }
Width :byte; { 字段长度 }
Decimals :byte; { 小数位数 }
end;
Type
TGuangFaDbf = class(TObject)
Private
{ Private Declarations }
DbfStream :TFileStream;
OldRecordNo:integer; //文件刚刚打开时的记录数
DbfHeadSize:integer; //文件头大小
DbfHead :Tdbfheader; //文件头记录
DbfField :Tdbffield; // 字段记录
DbfSize :integer; //文件大小
FieldCount :integer; //字段数
FieldSize :integer; //字段记录大小
RecordCount:integer; //记录数
RecordSize :integer; //记录大小
RecordNo :integer; //字段序号
DbfState :integer; //文件状态情况 0:read 1:write 2:append;
DbfChanged :integer; //文件修改情况 0:not changed, 1 change and no save
Fields : array of TtempField;//临时字段数组
CurrentRecord : array of char; //存放当前记录
Year,Month,Day: integer; //文件时间
public
Function DbfOpen(Filename:string):boolean;
Procedure DbfClose;
Function DbfGetFieldName(FieldNo:integer;Var FieldValue:string):boolean;
Function DbfGetFieldNo(FieldName:string):integer;
Function DbfGetFieldCount:integer;
Function DbfFreshCount(count:integer):boolean;
Function DbfGetRecordCount:integer;
Function DbfGetRecordNo:integer;
Function DbfGo(RecNum:integer):boolean;
Function DbfGoTop:boolean;
Function DbfGoBottom:boolean;
Function DbfSkip(step:integer):boolean;
Function DbfGetFieldByName(FieldName:string;Var FieldValue:string):Boolean;
Function DbfAssignChar(FieldName:string;FieldValue:string):boolean;
Function DbfPost:boolean;
Function DbfCancel:boolean;
Function DbfEdit:boolean;
Function DbfAppend:boolean;
end;

Const
Dbf_Read = 0;
Dbf_edit = 1;
Dbf_append = 2;

implementation

//打开dbf文件
function TGuangFaDbf.DbfOpen(Filename: string): boolean;
var
i:integer;
fieldoffset:integer;
s:byte;
begin
result:=true;
try
DbfStream:=TFileStream.Create(Filename,fmOpenReadWrite or fmShareDenyNone);
except
MessageBox(0,'打开文件出错','错误',MB_OK+MB_ICONWARNING);
result:=false;
exit;
end;
FillChar(DbfHead,sizeof(dbfhead),0);
if DbfStream.Read(dbfhead,32)<>32
then begin
MessageBox(0,'打开文件出错','错误',MB_OK+MB_ICONWARNING);
result:=false;
exit;
end;
if DbfHead.VersionNumber<>$03
then begin
MessageBox(0,'这个文件不是DBF文件','错误',MB_OK+MB_ICONWARNING);
result:=false;
exit;
end;
DbfSize := DbfStream.Size; //数据文件大小
FieldSize := sizeof(TdbfField); //字段记录大小
DbfHeadSize:= DbfHead.BytesInHeader; //文件头数据结构占用的长度
RecordSize := DbfHead.BytesInRecords; //每条记录大小
RecordCount:= DbfHead.NumberOfRecords; //记录数
OldRecordNo:=RecordCount; //文件刚刚打开事的记录数
FieldCount := (DbfHead.BytesInHeader-32) div 32; //字段数
SetLength(fields,fieldcount); //为临时字段记录分配空间
SetLength(CurrentRecord,RecordSize); //为当前记录分配空间
DbfState := Dbf_Read; //文件状态为读状态
Year := DbfHead.LastUpdateYear+1900; //文件最后修改年份
Month := DbfHead.LastUpdateMonth; //文件最后修改月份
Day := DbfHead.LastUpdateDay; //文件最后修改天份

fieldoffset:=1;
for i:=0 to fieldcount-1 do //读入字段定义数据
begin
FillChar(DbfField,sizeof(DbfField),0);
if DbfStream.Read(DbfField,32)<>32
then begin
DbfStream.Free;
MessageBox(0,'读文件失败','错误',MB_OK+MB_ICONWARNING);
result:=false;
exit;
end
else begin
move(DbfField.FieldName,fields.FieldName,11);
fields.FieldType:=dbffield.FieldType;
fields.fld_addr :=fieldoffset;
fields.Width :=dbffield.Width;
fields.Decimals :=dbffield.Decimals;
fieldoffset:=fieldoffset+dbffield.Width;
end;
end;// end of for i:=0 to .....
DbfStream.Read(s,1);
if DbfStream.Read(CurrentRecord[0],RecordSize)<>RecordSize
then RecordNo:=0
else RecordNo:=1;
end;

//关闭文件
Procedure TGuangFaDbf.DbfClose;
begin
if DbfStream<>nil
then DbfStream.Free;
end;

//取得字段名
Function TGuangFaDbf.DbfGetFieldName(FieldNo:integer;Var FieldValue:string):boolean;
var
FldName:array[0..10] of char;
begin
if FieldNo<0
then begin
result:=false;
exit;
end;
if FieldNo> DbfGetFieldCount-1
then begin
result:=false;
exit;
end;
try
FillChar(FldName,11,0);
move(Fields[FieldNo].FieldName[0],FldName[0],11);
FieldValue:=trim(FldName);
result:=true;
except
result:=false;
end;
end;

//取得字段的序号
Function TGuangFaDbf.DbfGetFieldNo(FieldName:string):integer;
var
fNo:integer;
s:string;
begin
fNo:=0;
repeat
s:=trim(Fields[fNo].FieldName);
if CompareText(FieldName,s)=0
then begin
result:=fNo;
break;
end
else begin
result:=-1;
fNo:=fNo+1;
end;
until fNo=FieldCount;
end;

//取文件的字段数
Function TGuangFaDbf.DbfGetFieldCount:integer;
begin
if DbfStream=nil
then result:=-1
else result:=FieldCount;
end;


//更新头结构中记录数
Function TGuangFaDbf.DbfFreshCount(count:integer):boolean;
var
DbfHead_var:TDbfHeader;
begin
result:=true;
if DbfStream=nil
then begin
result:=false;
exit;
end;
if DbfStream.Seek(0,soFromBeginning)<>0
then begin
result:=false;
exit;
end;
FillChar(DbfHead_Var,sizeof(DbfHead_Var),0);
if DbfStream.Read(DbfHead_var,32)<>32
then begin
result:=false;
exit;
end;
DbfHead_var.NumberOfRecords:=DbfHead_var.NumberOfRecords+count;
if DbfStream.Seek(0,soFromBeginning)<>0
then begin
result:=false;
exit;
end;
if DbfStream.Write(DbfHead_var,32)<>32
then begin
result:=false;
exit;
end;
RecordCount:=RecordCount+count;
end;

//取文件的记录数
Function TGuangFaDbf.DbfGetRecordCount:integer;
begin
if DbfStream=nil
then result:=-1
else result:=RecordCount;
end;

//取得目前记录号
Function TGuangFaDbf.DbfGetRecordNo:integer;
begin
if DbfStream=nil
then result:=-1
else result:=RecordNo;
end;

//将DBF的当前记录移动到指定记录号
Function TGuangFaDbf.DbfGo(RecNum:integer):boolean;
var
offset:integer;
begin
if RecNum<1
then begin
MessageBox(0,'记录号有错误','错误',MB_OK+MB_ICONWARNING);
result:=false;
exit;
end;
if RecNum>RecordCount
then begin
MessageBox(0,'记录号有错误','错误',MB_OK+MB_ICONWARNING);
result:=false;
exit;
end;
offset:=DbfHeadSize+(RecNum-1)*RecordSize;
if DbfStream.Seek(offset,soFromBeginning)<>offset
then begin
result:=false;
exit;
end;
if DbfStream.Read(CurrentRecord[0],RecordSize)<>RecordSize
then begin
RecordNo:=RecordNo;
result:=false;
end
else begin
result:=true;
RecordNo:=RecNum;
end;
end;

//移动记录指针到第一条记录
Function TGuangFaDbf.DbfGoTop:boolean;
begin
result:= DbfGo(1);
end;

//移动记录指针到最后一条记录
Function TGuangFaDbf.DbfGoBottom:boolean;
begin
if RecordCount<1
then begin
result:=false;
exit;
end;
result:= DbfGo(RecordCount);
end;

//将当前记录指针移动 step 条记录
Function TGuangFaDbf.DbfSkip(step:integer):boolean;
var
NewPos : integer;
offset : integer;
begin
NewPos:=RecordNo+step;
if NewPos<1
then NewPos:=1;
if NewPos>RecordCount
then NewPos:=RecordCount;
offset:=DbfHeadSize+(NewPos-1)*RecordSize;
if DbfStream.Seek(offset,soFromBeginning)<>offset
then begin
result:=false;
exit;
end;
if DbfStream.Read(CurrentRecord[0],RecordSize)<>RecordSize
then begin
result:=false;
exit;
end;
RecordNo:=NewPos;
result:=true;
end;

//通过给定字段名取字段值
Function TGuangFaDbf.DbfGetFieldByName(FieldName:string;Var FieldValue:string):Boolean;
var
buf:array of char;
FldValue:string;
i:integer;
FldNo : integer;
offset : integer;
FldSize: integer;
begin
FldNo:= DbfGetFieldNo(FieldName);
if FldNo=-1
then begin
result:=false;
exit;
end;
try
offset:=DbfHeadSize+(RecordNo-1)*RecordSize;
if DbfStream.Seek(offset,soFromBeginning)<>offset
then begin
result:=false;
exit;
end;
if DbfStream.Read(CurrentRecord[0],RecordSize)<>RecordSize
then begin
result:=false;
exit;
end;
//字段在记录中的偏移量
offset:=Fields[FldNo].fld_addr;
FldSize:=Fields[FldNo].Width;
SetLength(buf,FldSize);
SetLength(FldValue,FldSize);
Move(CurrentRecord[offset],buf[0],FldSize);
for i:=0 to FldSize-1 do
begin
FldValue[i+1]:=buf;
end;
FieldValue:=trim(FldValue);
Finalize(buf);
Finalize(FldValue);
result:=true;
except
result:=false;
end;
end;

//给当前记录中的字符型字段赋值
Function TGuangFaDbf.DbfAssignChar(FieldName:string;FieldValue:string):boolean;
var
offset:integer;
FldNo:integer;
ValueLen:integer;
FldValue:array of char;
i:integer;
begin
DbfChanged:=0;
FldNo:= DbfGetFieldNo(FieldName);
if FldNo=-1
then begin
result:=false;
exit;
end;
try
ValueLen:=length(FieldValue);
if ValueLen>Fields[FldNo].Width
then begin
ValueLen:=Fields[FldNo].Width;
end;
SetLength(FldValue,ValueLen);
for i:=0 to ValueLen-1 do
FldValue:=FieldValue[i+1];
offset:=Fields[FldNo].fld_addr;
//清空
Fillchar(CurrentRecord[offset],Fields[FldNo].Width,0);
//赋值
Move(FldValue[0],CurrentRecord[offset],ValueLen);
Finalize(FldValue);
DbfChanged:=1;
result:=true;
except
Finalize(FldValue);
result:=false;
exit;
end;
end;

//确认对当前记录的修改,将当前记录缓冲区写入到文件
Function TGuangFaDbf.DbfPost:boolean;
var
offset:integer;
EndByte:Byte;
begin
if DbfChanged<>1
then begin
result:=false;
exit;
end;
if DbfState=Dbf_Edit
then begin
offset:=DbfHead.BytesInHeader+(RecordNo-1)*RecordSize;
if DbfStream.Seek(offset,soFromBeginning)<>offset
then begin
result:=false;
exit;
end;
end;
if DbfState=Dbf_Append
then begin
offset:=RecordCount*RecordSize+DbfHeadSize+1;;
//Dbf文件的最后一位是$1A
if DbfStream.Seek(offset-1,soFromBeginning)<>offset-1
then begin
result:=false;
exit;
end;
end;
if DbfStream.Write(CurrentRecord[0],RecordSize)<>RecordSize
then begin
result:=false;
exit;
end;
if DbfState=Dbf_Append
then begin
EndByte:=$1A;
if DbfStream.Write(EndByte,1)<>1
then begin
result:=false;
exit;
end;
if not DbfFreshCount(1)
then begin
result:=false;
exit;
end;
end;
DbfState:=Dbf_Read;
DbfChanged:=0;
result:=true;
end;

//取消对当前记录的修改或者append,并从文件中读出原有字段值
Function TGuangFaDbf.DbfCancel:boolean;
begin
DbfState:=Dbf_Read;
DbfChanged:=0;
result:= DbfGo(RecordNo);
end;

//开始修改当前记录,修改完毕必须调用dbfpost确认或者dbfcancel取消
Function TGuangFaDbf.DbfEdit:boolean;
begin
DbfState:=Dbf_Edit;
result:= DbfGo(RecordNo);
end;

//开始修改当前记录,修改完毕必须调用dbfpost确认或者dbfcancel取消
Function TGuangFaDbf.DbfAppend:boolean;
begin
try
DbfState:=Dbf_Append;
Fillchar(CurrentRecord[0],RecordSize,0);
result:=true;
except
result:=false;
end;
end;
end.
 
to hua8hua:
能不能给我个例程(读和写数据)?
 
to hua8hua:
好象测试通不过!打不开DBF文件!
 
access 用ado即可,delphi5的
 
多人接受答案了。
 
后退
顶部