窗体里有一个button,一个adoconnection,一个adoquery,sql语句里加入变量的值,为什么不行呢?如何解决加变量这个问题?各位高手帮我看看
procedure TForm1.Button1Click(Sender: TObject);
var
n,o :array of string;
sql :string;
i :integer;
begin
setlength(n,10);
setlength(o,10);
for i :=0 to 5 do
begin
n[i] :=inttostr(2*i);
o[i] :=inttostr(2*i+1);
end;
adoconnection1.Connected :=true;
adoquery1.Connection :=adoconnection1;
for i :=0 to 5 do
begin
sql :='insert into dbo.tmptable';
sql :=sql +' values(';
sql :=sql +'n[i]';---------------------赋变量值为什么不行?
sql :=sql +',';
sql :=sql +'o[i]';
sql :=sql +')';
sql :=sql ;
adoquery1.Active :=false;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(sql);
adoquery1.ExecSQL;
end;end;
procedure TForm1.Button1Click(Sender: TObject);
var
n,o :array of string;
sql :string;
i :integer;
begin
setlength(n,10);
setlength(o,10);
for i :=0 to 5 do
begin
n[i] :=inttostr(2*i);
o[i] :=inttostr(2*i+1);
end;
adoconnection1.Connected :=true;
adoquery1.Connection :=adoconnection1;
for i :=0 to 5 do
begin
sql :='insert into dbo.tmptable';
sql :=sql +' values(';
sql :=sql +'n[i]';---------------------赋变量值为什么不行?
sql :=sql +',';
sql :=sql +'o[i]';
sql :=sql +')';
sql :=sql ;
adoquery1.Active :=false;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(sql);
adoquery1.ExecSQL;
end;end;
begin
sql :='insert into dbo.tmptable';
sql :=sql +' values(';
sql :=sql + n[i];---------------------赋变量值为什么不行?把单引号去掉
sql :=sql +',';
sql :=sql +o[i]; //把单引号去掉
sql :=sql +')';
sql :=sql ;
adoquery1.Active :=false;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(sql);
adoquery1.ExecSQL;
end;
var
n,o :array of string;
sql :string;
i :integer;
begin
setlength(n,10);
setlength(o,10);
for i :=0 to 5 do
begin
n[i] :=inttostr(2*i);
o[i] :=inttostr(2*i+1);
end;
adoconnection1.Connected :=true;
adoquery1.Connection :=adoconnection1;
for i :=0 to 5 do
begin
sql :='insert into dbo.tmptable';
sql :=sql +' values(';
sql :=sql +n[i];//////////////////把引号去了
sql :=sql +',';
sql :=sql +o[i];//////////////////把引号去了
sql :=sql +')';
sql :=sql ;
adoquery1.Active :=false;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(sql);
adoquery1.ExecSQL;
end;end;
如果这两个字段是字符串类型的,写成这样
procedure TForm1.Button1Click(Sender: TObject);
var
n,o :array of string;
sql :string;
i :integer;
begin
setlength(n,10);
setlength(o,10);
for i :=0 to 5 do
begin
n[i] :=inttostr(2*i);
o[i] :=inttostr(2*i+1);
end;
adoconnection1.Connected :=true;
adoquery1.Connection :=adoconnection1;
for i :=0 to 5 do
begin
sql :='insert into dbo.tmptable';
sql :=sql +' values(';
sql :=sql +''''+n[i]+'''';//////////////////
sql :=sql +',';
sql :=sql +''''+o[i]+'''';////////
sql :=sql +')';
sql :=sql ;
adoquery1.Active :=false;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(sql);
adoquery1.ExecSQL;
end;end;
速度和资源上比再建立一个adoquery快
如果是integer之类的,在SQL语句中是'+aa+',执行时就是直接是aa的值。
注:aa是变量名称。