结构体初始化方法:结构体存入Access数据库的方法



    懒得修改了参数里应该再加个字段名称有兴趣修改目前是根据VersionNO来判断时候也可以根据数据库中流长度来比较最好是通过外界手段自己控制吧

function TDataModuleDataBase.LoadRecFromDB(const aName, TableName: ;
  var Rec; RecSize: Integer): Boolean;
var
  MS: TMemoryStream;
begin
  Result := False;
  Trim(aName)=\'\' then
    Exit;
  MS := TMemoryStream.Create;
  try
    try
      ADOConnection.Open;
    except
      Exit;
    end;
    with ADOQuery do begin
      Active:=False;
      SQL.Text:=\'SELECT * FROM \'+ TableName +\' WHERE Name=\'+\'\'\'\'+ aName +\'\'\'\' + \' and AppVersion=\' + \'\'\'\' + VersionNO + \'\'\'\';
      Active:=True;
      RecordCount <> 1 then
        Exit;
      with FieldByName(\'RecOLE\') as TBlobField do begin
        MS.Position := 0;
        SaveToStream(MS);
        MS.Position := 0;
        MS.Read(Rec, RecSize);
      end;
    end;
  finally
    ADOConnection.Close;
    MS.Free;
  end;
  Result := True;
end;

 

 

function TDataModuleDataBase.SaveRecToDB(const aName, TableName: ;
  var Rec; RecSize: Integer): Boolean;
var
  MS: TMemoryStream;
begin
  Result := False;
  MS := TMemoryStream.Create;
  try
    try
      ADOConnection.Open;
    except
      Exit;
    end;
    with ADOQuery do begin
      Active:=False;
      SQL.Clear;
      SQL.Text:=\'SELECT * FROM \' + TableName + \' WHERE Name=\'+\'\'\'\'+ aName + \'\'\'\';
      Active:=True;
      RecordCount>=1 then
        Exit;
      Active:=False;
      SQL.Text:=\'SELECT * FROM \' + TableName;
      Prepared;
      Open;
      Append;[Page]
      FieldByName(\'Name\').Value := aName;
      FieldByName(\'AppVersion\').Value := VersionNO;
      with FieldByName(\'RecOLE\') as TBlobField do begin
        MS.Position := 0;
        MS.Write(Rec, RecSize);
        MS.Position := 0;
        LoadFromStream(MS);
      end;
      Post;
    end;
  finally
    ADOConnection.Close;
    MS.Free;
  end;
  Result := True;
end; 

Tags:  结构体与类的区别 结构体指针 结构体 结构体初始化方法

延伸阅读

最新评论

发表评论