微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

delphi手动创建dataset并插入值

unit Unit1;

interface

uses
  Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,
Vcl.Controls,Vcl.Forms,Vcl.Dialogs,DB,DBClient,Vcl.Grids,Vcl.DBGrids;

type
TForm1 = class(TForm)
dbGrd1: TDBGrid;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
class function AddDataToSet(AdsData: TDataSet): TDataSet;
class function CreateDataSet(dstemp:TDataSet): TDataSet;
end;

var
Form1: TForm1;

implementation


//创建dataset
class function TForm1.CreateDataSet(dstemp:TDataSet): TDataSet;
var
cdstemp: TClientDataSet;
begin
try
//创建DataSet
cdstemp := TClientDataSet.Create(Application);
if dstemp.FieldDefs <> nil then
begin
cdstemp.FieldDefs.Assign(dstemp.FieldDefs);
cdstemp.CreateDataSet;
result := (cdstemp as TDataSet);
end;
finally
//内存释放
dstemp.Free;
end;
end;


class function TForm1.AddDataToSet(AdsData: TDataSet): TDataSet;
var
intLoop:Integer;
begin
//打开数据集
AdsData.Open;
with AdsData do
begin
for intLoop := 0 to 10 do
begin
Append;//添加
FieldByName(‘Code‘).Asstring := ‘Code‘ + intToStr(intLoop);
FieldByName(‘Name‘).Asstring := ‘Name‘ + intToStr(intLoop);
FieldByName(‘Code‘).AsInteger := intLoop;
post;//提交
end;
end;
end;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
dstemp:TDataSet;
begin
//初始化
dstemp := TDataSet.Create(Application);
with dstemp.FieldDefs do
begin
Add(‘code‘,ftString,8);
Add(‘name‘,20);
Add(‘Number‘,ftInteger);
end;
dstemp:=TForm1.CreateDataSet(dstemp);
TForm1.AddDataToSet(dstemp);

dstemp.Open;
while not dstemp.Eof do
begin
showmessage(string(dstemp.FieldByName(‘Name‘).Value)) ;
dstemp.Next ;
end ;

end;

end.
---------------------

DataSet有两个东西,一个是表结构FieldDefs,一个是TClientDataSet。这个.net还是有一些不同。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐