delphi 中如何执行SqlParameter形式的SQL语句

procedure TForm1.Button1Click(Sender: TObject);
begin
ADOConnection1.Open('xj','');
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into UserInfo(UserName, UserPwd, Address, RoleID, IsDel, CreateDate) values(:UserName, :UserPwd, :Address, :RoleID, :IsDel, :CreateDate)');
ADOQuery1.Parameters.Clear;
ADOQuery1.Parameters.CreateParameter('UserName',ftString,pdInput,,'aaa');
ADOQuery1.Parameters.CreateParameter('UserPwd',ftString,pdInput,,'aaa1');
ADOQuery1.Parameters.CreateParameter('Address',ftString,pdInput,,'aaa2');
ADOQuery1.Parameters.CreateParameter('RoleID',ftInteger,pdInput,,);
ADOQuery1.Parameters.CreateParameter('IsDel',ftInteger,pdInput,,);
ADOQuery1.Parameters.CreateParameter('CreateDate',ftDateTime,pdInput,,null);
ADOQuery1.ExecSQL;
ADOConnection1.Close;
end;
也可以通过预编译执行SQL:
procedure Takhbdsp.Button1Click(Sender: TObject);
begin
ADOConnection1.Open('xj','');
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into UserInfo(UserName, UserPwd, Address, RoleID, IsDel, CreateDate) values(:UserName, :UserPwd, :Address, :RoleID, :IsDel, :CreateDate)');
ADOQuery1.Prepared:=True;//预编译置为True
{
ADOQuery1.Parameters.Clear;
ADOQuery1.Parameters.CreateParameter('UserName',ftString,pdInput,50,UserInfo.UserName);
ADOQuery1.Parameters.CreateParameter('UserPwd',ftString,pdInput,50,UserInfo.UserPwd);
ADOQuery1.Parameters.CreateParameter('Address',ftString,pdInput,50,UserInfo.Address);
ADOQuery1.Parameters.CreateParameter('RoleID',ftString,pdInput,50,UserInfo.RoleID);
ADOQuery1.Parameters.CreateParameter('IsDel',ftString,pdInput,50,UserInfo.IsDel);
ADOQuery1.Parameters.CreateParameter('CreateDate',ftString,pdInput,50,UserInfo.CreateDate);
}
//不用创建Parameter,而是直接给Parameter赋值,通常都采用这种方法,预编译能够优化SQL语句,提高执行效率。
ADOQuery1.Parameters[].Value:='aaaun';
ADOQuery1.Parameters[].Value:='aaapwd';
ADOQuery1.Parameters[].Value:='aaaadress';
ADOQuery1.Parameters[].Value:='';
ADOQuery1.Parameters[].Value:='';
ADOQuery1.Parameters[].Value:='2008-9-18';
ADOQuery1.ExecSQL;
ADOConnection1.Close;
end;
上一篇:(转)利用JConsole工具监控java程序内存和JVM


下一篇:RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->Web版本“产品管理”事例编辑界面新增KindEditor复文本编辑控件