//此文件为类声明文件
unit Unit2;interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ADODB;
type  LEmployee = class
  private
    _name : string;
    _age : integer;
  public
    procedure setName(n:string);
    procedure setAge(a:integer);
    procedure AddEmp(AnAdoQuery:TAdoQuery);
    function getName():string;
    function getAge():integer;
  end;
implementation{ LEmployee }procedure LEmployee.AddEmp(AnAdoQuery: TAdoQuery);
begin
  AnAdoQuery.Connection.BeginTrans;
  try
  AnAdoQuery.Close;
  AnAdoQuery.SQL.Clear;
  AnAdoQuery.SQL.Add('insert into EInfo (name,age) values (:n,:a)');
  AnAdoQuery.Parameters.ParamByName('n').value := _name;
  AnAdoQuery.Parameters.ParamByName('a').value := _age;
  AnAdoQuery.ExecSQL;
  AnAdoQuery.Connection.CommitTrans;
  except
  showmessage('error');
  AnAdoQuery.Connection.RollbackTrans;
  end;
end;function LEmployee.getAge: integer;
begin
  result := _age;
end;function LEmployee.getName: string;
begin
  result := _name;
end;procedure LEmployee.setAge(a:integer);
begin
  _age := a;
end;procedure LEmployee.setName(n:string);
begin
  _name := n;
end;end.//窗体文件unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Unit2;type
  TForm1 = class(TForm)
    ADOQuery1: TADOQuery;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Emp : LEmployee;
  end;var
  Form1: TForm1;implementationuses Unit3;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  Emp := LEmployee.Create;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  Emp.setName(edit1.Text);
  Emp.setAge(StrToInt(edit2.Text));
  Emp.AddEmp(ADOQuery1);
end;end.
//数据模块文件,一个ADOconnection控件连接数据库
unit Unit3;interfaceuses
  SysUtils, Classes, DB, ADODB;type
  TDataModule3 = class(TDataModule)
    ADOConnection1: TADOConnection;
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  DataModule3: TDataModule3;implementation{$R *.dfm}end.
看看那里有错,反正是有问题!编译没问题,运行有问题!
错误信息:
Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00478f31 in module 'project1.exe'.Read of address 0000006C'.Process stopped.Use step or Run to continue.

解决方案 »

  1.   

    LEmployee类没有Create和Destroy方法!
      

  2.   

    Create 和 Destroy是自动就有的...
      

  3.   

    应该是这里:procedure LEmployee.AddEmp(AnAdoQuery: TAdoQuery);
    begin
      AnAdoQuery.Connection.BeginTrans;
      try
      AnAdoQuery.Close;
      AnAdoQuery.SQL.Clear;
      AnAdoQuery.SQL.Add('insert into EInfo (name,age) values (:n,:a)');
      AnAdoQuery.Parameters.ParamByName('n').value := _name;
      AnAdoQuery.Parameters.ParamByName('a').value := _age;
      AnAdoQuery.ExecSQL;
      AnAdoQuery.Connection.CommitTrans;
      except
      showmessage('error');
      AnAdoQuery.Connection.RollbackTrans;
      end;
    end;的问题
      

  4.   

    急啊!!!!!up up up up !