akalb
2009-04-28 07:23:23 UTC
create a Table with 2 fields:
ID as integer
MyBlob as Memo
define a type like this
type
TUser = record
Name : string[2];
Cognome : string[4];
Eta : integer;
DataNascita : TDateTime;
end;
form definition like this
TRecord2BlobForm = class(TForm)
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
InsertButton: TButton;
myTable: TADOTable;
edNOQ: TEdit;
DBNavigator1: TDBNavigator;
myTableID: TIntegerField;
myTableCampoBlob: TMemoField;
Edit_Cognome: TLabeledEdit;
Edit_Nome: TLabeledEdit;
Edit_Eta: TLabeledEdit;
Edit_DataNascita: TDateTimePicker;
Label1: TLabel;
procedure InsertButtonClick(Sender: TObject);
procedure myTableAfterScroll(DataSet: TDataSet);
procedure TRecord2BlobForm.myTableAfterScroll(DataSet: TDataSet);
var
User : TUser;
blobF : TBlobField;
bs : TStream;
begin
if myTable.State in [dsInsert, dsEdit] then Exit;
if myTable.FieldByName('MyBlob').IsBlob then
begin
blobF := DataSet.FieldByName('MyBlob') as TBlobField;
bs := myTable.CreateBlobStream(blobF, bmRead);
try
bs.Read(User,sizeof(TUser));
finally
bs.Free;
end;
end;
Edit_Nome.Text := User.Name;
Edit_Cognome.Text := User.Cognome;
Edit_Eta.Text := IntToStr(User.Eta);
Edit_DataNascita.DateTime := User.DataNascita;
end;
procedure TRecord2BlobForm.InsertButtonClick(Sender: TObject);
var
User : TUser;
blobF : TBlobField;
bs : TStream;
begin
User.Name := Edit_Nome.Text;
User.Cognome := Edit_Cognome.Text;
User.Eta := StrToInt(Edit_Eta.Text);
User.DataNascita := Edit_DataNascita.DateTime;
myTable.Insert;
//assign the key field
myTable.FieldByName('id').AsInteger := myTable.RecordCount;
blobF := myTable.FieldByName('MyBlob') as TBlobField;
bs := myTable.CreateBlobStream(blobF, bmWrite);
try
bs.Write(User,SizeOf(TUser));
finally
bs.Free;
end;
myTable.Post;
end;
end.
ID as integer
MyBlob as Memo
define a type like this
type
TUser = record
Name : string[2];
Cognome : string[4];
Eta : integer;
DataNascita : TDateTime;
end;
form definition like this
TRecord2BlobForm = class(TForm)
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
InsertButton: TButton;
myTable: TADOTable;
edNOQ: TEdit;
DBNavigator1: TDBNavigator;
myTableID: TIntegerField;
myTableCampoBlob: TMemoField;
Edit_Cognome: TLabeledEdit;
Edit_Nome: TLabeledEdit;
Edit_Eta: TLabeledEdit;
Edit_DataNascita: TDateTimePicker;
Label1: TLabel;
procedure InsertButtonClick(Sender: TObject);
procedure myTableAfterScroll(DataSet: TDataSet);
procedure TRecord2BlobForm.myTableAfterScroll(DataSet: TDataSet);
var
User : TUser;
blobF : TBlobField;
bs : TStream;
begin
if myTable.State in [dsInsert, dsEdit] then Exit;
if myTable.FieldByName('MyBlob').IsBlob then
begin
blobF := DataSet.FieldByName('MyBlob') as TBlobField;
bs := myTable.CreateBlobStream(blobF, bmRead);
try
bs.Read(User,sizeof(TUser));
finally
bs.Free;
end;
end;
Edit_Nome.Text := User.Name;
Edit_Cognome.Text := User.Cognome;
Edit_Eta.Text := IntToStr(User.Eta);
Edit_DataNascita.DateTime := User.DataNascita;
end;
procedure TRecord2BlobForm.InsertButtonClick(Sender: TObject);
var
User : TUser;
blobF : TBlobField;
bs : TStream;
begin
User.Name := Edit_Nome.Text;
User.Cognome := Edit_Cognome.Text;
User.Eta := StrToInt(Edit_Eta.Text);
User.DataNascita := Edit_DataNascita.DateTime;
myTable.Insert;
//assign the key field
myTable.FieldByName('id').AsInteger := myTable.RecordCount;
blobF := myTable.FieldByName('MyBlob') as TBlobField;
bs := myTable.CreateBlobStream(blobF, bmWrite);
try
bs.Write(User,SizeOf(TUser));
finally
bs.Free;
end;
myTable.Post;
end;
end.