高手们,帮帮我!ADO编程,我怎么才能执行一个SQL语句,如我要执行Select f1,f2 form data where id=12 并且要将获取的值存入str1,str2变量中,怎么做呀?小弟初学Delphi,还望各位多多指教!
with ADOQuery do
begin
Active:=False;
Sql.Clear;
Sql.Add( 'Select f1,f2 form data where id=12');
Active:=True;
Str1 := FieldByName('f1').AsString;
Str2 := FieldByName('f2').AsString;
end;
var
str1:string;
str2:string;
begin
Adoquery1.close;
adoquery1.sql.text:='select f1,f2 from data where id=12';
str1:=adoquery1.fieldbyname('f1').asstring;
str2:=adoquery1.fieldbyname('f2').asstring;
end;
with ADOQuery do
begin
Active:=False;
Sql.Clear;
Sql.Add( 'Select f1,f2 form data where id=12');
Active:=True;
Str1 := FieldByName('f1').AsString;
Str2 := FieldByName('f2').AsString;
end;
with ADOQuery do
begin
Close;
Sql.Text := 'Select f1,f2 form data where id=12';
Open;
while not Eof do
begin
Str1 := FieldByName('f1').AsString;
Str2 := FieldByName('f2').AsString;
//处理自己的操作
Next;
end;
--也可以用Fetch来做,如果你用存储过程实现,使用
--SQL SERVER光标(游标,CURSOR)也可以实现,语句如下:
use pubs
declare @s1 varchar(50),@s2 varchar(50)
declare dd cursor scroll for
select title_id,title from titles
--Select f1,f2 form data where id=12 在你程序里,请用此行替换上一行select
open dd
fetch first from dd into @s1,@s2
close dd
deallocate dd
先取一个ADOQUERY,不要忘记设置CONNECTSTRING的属性.
adoquery.close;
adoquery.sql.text:='select f1,f2 from data where id=12 '
adoquery.open;
str1:=adoquery.fieldbyname('f1').asstring;
str2:=adoquery.fieldbyname('f2').asstring;
这样大概就可以了;