M
microrain
Unregistered / Unconfirmed
GUEST, unregistred user!
我准备写一个封装ADO操作的类。可是老是出错,请大家看一看下面的代码。为什么老是出错呀
unit Eado;
interface
uses
Windows, Messages, SysUtils,ADODB_TLB, ComObj,Forms;
type
TEAdo=class
private
objConn:OleVariant;
RecordSet:OleVariant;
spath:String;
rec:string;
function opendb:Boolean;
procedure closedb;
public
constructor Create;
//destructor Destroy;
function AddRecord(StrSQL:String):Boolean;
function DelRecord(nIndex:integer):Boolean;
end;
implementation
constructor TEAdo.Create;
begin
objConn:=CreateOleObject('ADODB.Connection');
RecordSet:=CreateOleObject('ADODB.Recordset');
end;
{* 对数据库进行初始化并打开数据库 *}
Function TEAdo.opendb:Boolean;
begin
try
spath:=extractfilepath(application.ExeName )+'data/clientdb.mdb';
objConn.open('driver={Microsoft Access Driver (*.mdb)};dbq='+spath);
result:=true;
finally
result:=false;
end;
end;
{* 关闭数据库连接 *}
Procedure TEAdo.closedb;
begin
objConn.close;
end;
function TEAdo.AddRecord(StrSQL:String):Boolean;
begin
opendb;
try
RecordSet.Open(StrSQL,objConn,3,3)
//<<<=====这个位置出错了,说没有打开命令什么的?
RecordSet.AddNew;
RecordSet.Fields[1].Value:='55555555555';
RecordSet.Update();
RecordSet.Close;
result:=true;
finally
result:=false;
end;
end;
function TEAdo.DelRecord(nIndex:integer):Boolean;
begin
opendb;
result:=true;
end;
end.
调用的地方如下
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Eado, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyAdo:TEAdo;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MyAdo:=TEAdo.Create;
end;
procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
myado.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not myado.AddRecord('') then
ShowMessage('error');
end;
end.
unit Eado;
interface
uses
Windows, Messages, SysUtils,ADODB_TLB, ComObj,Forms;
type
TEAdo=class
private
objConn:OleVariant;
RecordSet:OleVariant;
spath:String;
rec:string;
function opendb:Boolean;
procedure closedb;
public
constructor Create;
//destructor Destroy;
function AddRecord(StrSQL:String):Boolean;
function DelRecord(nIndex:integer):Boolean;
end;
implementation
constructor TEAdo.Create;
begin
objConn:=CreateOleObject('ADODB.Connection');
RecordSet:=CreateOleObject('ADODB.Recordset');
end;
{* 对数据库进行初始化并打开数据库 *}
Function TEAdo.opendb:Boolean;
begin
try
spath:=extractfilepath(application.ExeName )+'data/clientdb.mdb';
objConn.open('driver={Microsoft Access Driver (*.mdb)};dbq='+spath);
result:=true;
finally
result:=false;
end;
end;
{* 关闭数据库连接 *}
Procedure TEAdo.closedb;
begin
objConn.close;
end;
function TEAdo.AddRecord(StrSQL:String):Boolean;
begin
opendb;
try
RecordSet.Open(StrSQL,objConn,3,3)
//<<<=====这个位置出错了,说没有打开命令什么的?
RecordSet.AddNew;
RecordSet.Fields[1].Value:='55555555555';
RecordSet.Update();
RecordSet.Close;
result:=true;
finally
result:=false;
end;
end;
function TEAdo.DelRecord(nIndex:integer):Boolean;
begin
opendb;
result:=true;
end;
end.
调用的地方如下
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Eado, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyAdo:TEAdo;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MyAdo:=TEAdo.Create;
end;
procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
myado.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not myado.AddRecord('') then
ShowMessage('error');
end;
end.