procedure TForm1.Button5Click(Sender: TObject);
var
index,i,j:Integer;
FilePath,Code,County,formula:string;
DB:Database;
DAOQuery2:TDAoquery;
function getprice(code,county,currentstr:string):string; //这里的code传入的是库编码,对数据库进行一次查询返回county
begin
with DAOQuery2 do
begin
Close;
SQL.Clear;
SQL.Add('select '+County+' from '+currentstr+' where 材料编码 in (select 材料编码 from MonDYB where 库编码='+QuotedStr(code)+')');
Open;
result:=FieldByName(County).AsString;
end;
end;
begin
with DAOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('select * into MonthPrice from Monformwork');// 复制临时表monthprice
Execute(0);
SQL.Clear;
SQL.Add('select * from MonthPrice');//怪临时表记录集
open;
First;//取第一条记录
while not eof do
begin
Code:=FieldByName('库编码').AsString;//取第一条记录集中的所需要的一个字段做为下面函数的参数
for i:=4 to fieldcount-1 do //对这条记录的的第五个字段至第最后一个字段循环进行处理
begin
County:=Fields[i].FieldName; //取第五个字段做为函数的参数
DAOQuery2.Create(Application); //动态建立一个query
DAOQuery2.Database:=DAODatabase1;
with DAOQuery2 do //进行第五个字段的处理
begin
Close;
SQL.Clear;
SQL.Add('select 转换公式 from MonDYB where 库编码='+QuotedStr(code));
Open;
formula:=FieldByName('转换公式').AsString;
index:=pos('{',formula);
while index > 0 do
begin
j:=pos('}',formula);
formula:=copy(formula,1,index-1)+getprice(copy(formula,index+1,j-index-1),County,currentstr)+copy(formula,j+1,length(formula)-j);
index:=pos('{',formula);
Close;
SQL.Clear;
SQL.Add('update Monthprice set '+County+'='+formula+' where 库编码='+QuotedStr(code));
Execute(0);
end;
end;
end;
end;
next;
end;
end;帮我看看哪里出问题了?
var
index,i,j:Integer;
FilePath,Code,County,formula:string;
DB:Database;
DAOQuery2:TDAoquery;
function getprice(code,county,currentstr:string):string; //这里的code传入的是库编码,对数据库进行一次查询返回county
begin
with DAOQuery2 do
begin
Close;
SQL.Clear;
SQL.Add('select '+County+' from '+currentstr+' where 材料编码 in (select 材料编码 from MonDYB where 库编码='+QuotedStr(code)+')');
Open;
result:=FieldByName(County).AsString;
end;
end;
begin
with DAOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('select * into MonthPrice from Monformwork');// 复制临时表monthprice
Execute(0);
SQL.Clear;
SQL.Add('select * from MonthPrice');//怪临时表记录集
open;
First;//取第一条记录
while not eof do
begin
Code:=FieldByName('库编码').AsString;//取第一条记录集中的所需要的一个字段做为下面函数的参数
for i:=4 to fieldcount-1 do //对这条记录的的第五个字段至第最后一个字段循环进行处理
begin
County:=Fields[i].FieldName; //取第五个字段做为函数的参数
DAOQuery2.Create(Application); //动态建立一个query
DAOQuery2.Database:=DAODatabase1;
with DAOQuery2 do //进行第五个字段的处理
begin
Close;
SQL.Clear;
SQL.Add('select 转换公式 from MonDYB where 库编码='+QuotedStr(code));
Open;
formula:=FieldByName('转换公式').AsString;
index:=pos('{',formula);
while index > 0 do
begin
j:=pos('}',formula);
formula:=copy(formula,1,index-1)+getprice(copy(formula,index+1,j-index-1),County,currentstr)+copy(formula,j+1,length(formula)-j);
index:=pos('{',formula);
Close;
SQL.Clear;
SQL.Add('update Monthprice set '+County+'='+formula+' where 库编码='+QuotedStr(code));
Execute(0);
end;
end;
end;
end;
next;
end;
end;帮我看看哪里出问题了?
Close;
SQL.Clear;
SQL.Add( 'select * into MonthPrice from Monformwork ');// 复制临时表monthprice
Execute(0);
SQL.Clear;
SQL.Add( 'select * from MonthPrice ');//怪临时表记录集
open;
//这句应该这样写
DAOQuery2:=TDAoquery.Create( Owner );
try
....
finally
DAOQuery2.free;
end;
另外你应该把DAOQuery2创建过程放到循环以外来做,在循环里频繁的创建与销毁对象效率非常低
上面那个动态创建为什么不能那样写?好象ADOquery可以是不是?
with TDAoquery.Create(owner) do
begin
.....
Destroy;
end;
begin
.....
Destroy;
end;这样效率会高些吗?
临时创建的组件最好使用
创建过程
try
...
finally
销毁
end;
var
index,i,j:Integer;
FilePath,Code,County,formula:string;
DB:Database;
DAOQuery2:TDAoquery;
function getprice(code,county,currentstr:string):string; //这里的code传入的是库编码,对数据库进行一次查询返回county
begin
with DAOQuery2 do
begin
Close;
SQL.Clear;
SQL.Add( 'select '+County+ ' from '+currentstr+ ' where 材料编码 in (select 材料编码 from MonDYB where 库编码= '+QuotedStr(code)+ ') ');
Open;
result:=FieldByName(County).AsString;
end;
end;
begin
with DAOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add( 'select * into MonthPrice from Monformwork ');// 复制临时表monthprice
Execute(0);
SQL.Clear;
SQL.Add( 'select * from MonthPrice ');//取临时表记录集
open;
First;//取第一条记录
while not eof do
begin
Code:=FieldByName( '库编码 ').AsString;//取第一条记录集中的所需要的一个字段做为下面函数的参数
for i:=4 to fieldcount-1 do //对这条记录的的第五个字段至第最后一个字段循环进行处理
begin
County:=Fields[i].FieldName; //取第五个字段做为函数的参数
with DAOQuery2 do //进行第五个字段的处理
begin
Close;
SQL.Clear;
SQL.Add( 'select 转换公式 from MonDYB where 库编码= '+QuotedStr(code));
Open;
formula:=FieldByName( '转换公式 ').AsString;
index:=pos( '{ ',formula);
while index > 0 do
begin
j:=pos( '} ',formula);
formula:=copy(formula,1,index-1)+getprice(copy(formula,index+1,j-index-1),County,currentstr)+copy(formula,j+1,length(formula)-j);
index:=pos( '{ ',formula);
Close;
SQL.Clear;
SQL.Add( 'update Monthprice set '+County+ '= '+formula+ ' where 库编码= '+QuotedStr(code));
Execute(0);
end;
end;
end;
end;
next;
end;
end; 我在界面上放了一个daodatabase1 一个daoquery1 一个daoquery2 都指向database1 程序改为上面那样不动态创建
还是会出现标题那个地址错误而改用
DAOQuery2.Create(Application); //动态建立一个query
//这句应该这样写
DAOQuery2:=TDAoquery.Create( Owner );
try
....
finally
DAOQuery2.free;
end;
这样就行,不会出现错误还有要把创建过程放到循环外来做要怎么做?
try
...
finally
这是没办法搬到循环外的