- procedure TForm1.Button1Click(Sender: TObject);
- begin
- ADOConnection1.Open('xj','123456');
- 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,50,'aaa');
- ADOQuery1.Parameters.CreateParameter('UserPwd',ftString,pdInput,50,'aaa1');
- ADOQuery1.Parameters.CreateParameter('Address',ftString,pdInput,50,'aaa2');
- ADOQuery1.Parameters.CreateParameter('RoleID',ftInteger,pdInput,50,1);
- ADOQuery1.Parameters.CreateParameter('IsDel',ftInteger,pdInput,50,0);
- ADOQuery1.Parameters.CreateParameter('CreateDate',ftDateTime,pdInput,50,null);
- ADOQuery1.ExecSQL;
- ADOConnection1.Close;
- end;
也可以通过预编译执行SQL:
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- ADOConnection1.Open('xj','123456');
- 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[0].Value:='aaaun';
- ADOQuery1.Parameters[1].Value:='aaapwd';
- ADOQuery1.Parameters[2].Value:='aaaadress';
- ADOQuery1.Parameters[3].Value:='1';
- ADOQuery1.Parameters[4].Value:='0';
- ADOQuery1.Parameters[5].Value:='2008-9-18';
- ADOQuery1.ExecSQL;
- ADOConnection1.Close;
- end;