01.procedure TForm1.Button1Click(Sender: TObject); 02.begin 03. ADOConnection1.Open('xj','123456'); 04. ADOQuery1.Close; 05. ADOQuery1.SQL.Clear; 06. ADOQuery1.SQL.Add('insert into UserInfo(UserName, UserPwd, Address, RoleID, IsDel, CreateDate) values(:UserName, :UserPwd, :Address, :RoleID, :IsDel, :CreateDate)'); 07. ADOQuery1.Parameters.Clear; 08. ADOQuery1.Parameters.CreateParameter('UserName',ftString,pdInput,50,'aaa'); 09. ADOQuery1.Parameters.CreateParameter('UserPwd',ftString,pdInput,50,'aaa1'); 10. ADOQuery1.Parameters.CreateParameter('Address',ftString,pdInput,50,'aaa2'); 11. ADOQuery1.Parameters.CreateParameter('RoleID',ftInteger,pdInput,50,1); 12. ADOQuery1.Parameters.CreateParameter('IsDel',ftInteger,pdInput,50,0); 13. ADOQuery1.Parameters.CreateParameter('CreateDate',ftDateTime,pdInput,50,null); 14. ADOQuery1.ExecSQL; 15. ADOConnection1.Close; 16.end;
也可以通过预编译执行SQL:
View Code
1 01.procedure TForm1.Button1Click(Sender: TObject); 2 02.begin 3 03. 4 04. ADOConnection1.Open('xj','123456'); 5 05. ADOQuery1.Close; 6 06. ADOQuery1.SQL.Clear; 7 07. ADOQuery1.SQL.Add('insert into UserInfo(UserName, UserPwd, Address, RoleID, IsDel, CreateDate) values(:UserName, :UserPwd, :Address, :RoleID, :IsDel, :CreateDate)'); 8 08. ADOQuery1.Prepared:=True;//预编译置为True 9 09. {10 10. ADOQuery1.Parameters.Clear;11 11. ADOQuery1.Parameters.CreateParameter('UserName',ftString,pdInput,50,UserInfo.UserName);12 12. ADOQuery1.Parameters.CreateParameter('UserPwd',ftString,pdInput,50,UserInfo.UserPwd);13 13. ADOQuery1.Parameters.CreateParameter('Address',ftString,pdInput,50,UserInfo.Address);14 14. ADOQuery1.Parameters.CreateParameter('RoleID',ftString,pdInput,50,UserInfo.RoleID);15 15. ADOQuery1.Parameters.CreateParameter('IsDel',ftString,pdInput,50,UserInfo.IsDel);16 16. ADOQuery1.Parameters.CreateParameter('CreateDate',ftString,pdInput,50,UserInfo.CreateDate);17 17. }18 18.//不用创建Parameter,而是直接给Parameter赋值,通常都采用这种方法,预编译能够优化SQL语句,提高执行效率。 19 19. ADOQuery1.Parameters[0].Value:='aaaun';20 20. ADOQuery1.Parameters[1].Value:='aaapwd';21 21. ADOQuery1.Parameters[2].Value:='aaaadress';22 22. ADOQuery1.Parameters[3].Value:='1';23 23. ADOQuery1.Parameters[4].Value:='0';24 24. ADOQuery1.Parameters[5].Value:='2008-9-18';25 25. ADOQuery1.ExecSQL;26 26. ADOConnection1.Close;27 27.28 28. 29 29.end;