Y
yanlei
Unregistered / Unconfirmed
GUEST, unregistred user!
com对象
unit utDatabaseconn;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ActiveX, Mtsobj, Mtx, ComObj, StdVcl,YgDatabaseConn_TLB,
windows, Registry, AdoDb, SysUtils,dialogs,Variants;
type
TDatabaseConn = class(TMtsAutoObject, IDatabaseConn)
private
Fconn: olevariant;
protected
function CanBePooled: BOOL;
override;
stdcall;
function adoconn: olevariant;
safecall;
public
procedure Initialize;
override;
destructor Destroy;
override;
end;
implementation
uses ComServ;
function TDatabaseConn.CanBePooled: BOOL;
begin
Result := True;
end;
function TDatabaseConn.adoconn: olevariant;
begin
Result := Fconn;
end;
procedure TDatabaseConn.Initialize;
const COMCONNPATH = '/Software/yjd/conn';
var
Connstr: string;
Ado:TadoConnection;
begin
inherited;
//初始化私有变量
try
with TRegistry.Createdo
begin
RootKey := HKEY_CURRENT_USER;
OpenKey(COMCONNPATH, False);
Connstr := ReadString('string');
CloseKey;
Free;
end;
if Connstr <> '' then
begin
Fconn := CreateOleObject('ADODB.Connection');
Fconn.ConnectionString := Connstr;
Fconn.Open('', '', '', -1);
showmessage(Connstr+'成功1');
end;
except
showmessage(Connstr+'失败');
end;
end;
destructor TDatabaseConn.Destroy;
begin
//释放私有变量
//FreeAndNil(conn);
inherited Destroy;
end;
initialization
TAutoObjectFactory.Create(ComServer, TDatabaseConn, Class_DatabaseConn,
ciMultiInstance, tmNeutral);
end.
调用
function VariantTo_Connection(const Value: OleVariant): _Connection;
begin
with TVarData(Value)do
if VType = varDispatch then
Result := IDispatch(TVarData(Value).VDispatch) as _Connection
else
Result := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Conn: _Connection;
DatabaseConn: IDatabaseConn;
Conntemp: _Connection;
RecordsAffected: OleVariant;
begin
DatabaseConn := CoDatabaseConn.CreateRemote('192.168.202.129') as
IDatabaseConn;
Conn := VariantTo_Connection(DatabaseConn.Conn);
showmessage(Conn.ConnectionString);
// Conn.Execute('create table st1 (id integer)',RecordsAffected,1);
ADOConnection1.ConnectionObject:=conn;
//这里出错
ADOConnection1.Connected:=true;
end;
ADOConnection1.ConnectionObject:=conn;
//这里出错
ole 80040202错误,谁能帮我一下
unit utDatabaseconn;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ActiveX, Mtsobj, Mtx, ComObj, StdVcl,YgDatabaseConn_TLB,
windows, Registry, AdoDb, SysUtils,dialogs,Variants;
type
TDatabaseConn = class(TMtsAutoObject, IDatabaseConn)
private
Fconn: olevariant;
protected
function CanBePooled: BOOL;
override;
stdcall;
function adoconn: olevariant;
safecall;
public
procedure Initialize;
override;
destructor Destroy;
override;
end;
implementation
uses ComServ;
function TDatabaseConn.CanBePooled: BOOL;
begin
Result := True;
end;
function TDatabaseConn.adoconn: olevariant;
begin
Result := Fconn;
end;
procedure TDatabaseConn.Initialize;
const COMCONNPATH = '/Software/yjd/conn';
var
Connstr: string;
Ado:TadoConnection;
begin
inherited;
//初始化私有变量
try
with TRegistry.Createdo
begin
RootKey := HKEY_CURRENT_USER;
OpenKey(COMCONNPATH, False);
Connstr := ReadString('string');
CloseKey;
Free;
end;
if Connstr <> '' then
begin
Fconn := CreateOleObject('ADODB.Connection');
Fconn.ConnectionString := Connstr;
Fconn.Open('', '', '', -1);
showmessage(Connstr+'成功1');
end;
except
showmessage(Connstr+'失败');
end;
end;
destructor TDatabaseConn.Destroy;
begin
//释放私有变量
//FreeAndNil(conn);
inherited Destroy;
end;
initialization
TAutoObjectFactory.Create(ComServer, TDatabaseConn, Class_DatabaseConn,
ciMultiInstance, tmNeutral);
end.
调用
function VariantTo_Connection(const Value: OleVariant): _Connection;
begin
with TVarData(Value)do
if VType = varDispatch then
Result := IDispatch(TVarData(Value).VDispatch) as _Connection
else
Result := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Conn: _Connection;
DatabaseConn: IDatabaseConn;
Conntemp: _Connection;
RecordsAffected: OleVariant;
begin
DatabaseConn := CoDatabaseConn.CreateRemote('192.168.202.129') as
IDatabaseConn;
Conn := VariantTo_Connection(DatabaseConn.Conn);
showmessage(Conn.ConnectionString);
// Conn.Execute('create table st1 (id integer)',RecordsAffected,1);
ADOConnection1.ConnectionObject:=conn;
//这里出错
ADOConnection1.Connected:=true;
end;
ADOConnection1.ConnectionObject:=conn;
//这里出错
ole 80040202错误,谁能帮我一下