//此文件为类声明文件
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.
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.
解决方案 »
- 请教一个关于浮点数比较的问题,对老答案的疑惑
- 熟悉Ehlib组件的高手进来帮忙看看(dxDBGrid多选的实现)
- 奇怪的PChar问题
- 请问各位大虾 delphi如何打包开发的工程
- 请问Database是如何连接SQL SERVER 数据库的
- 在DELPHI 中控制扫描仪
- 求API手册,请知道的帮忙,急用
- 求帮助~~~i/o error 103
- delphi的前景到底如何?我到底学delphi还是c#?十万火急请教各位兄弟!(我现在用java,jsp,asp,主要写b/s程序,我想充实自己学学c/s编程)
- 这个webbrowser问题有点难度和技巧和意思。
- 如何在Dbgrid中当鼠标双击时不处于编辑状态?
- 关于菜单
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;的问题