com+,期待着你光临(300分)

  • 主题发起人 主题发起人 yanlei
  • 开始时间 开始时间
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错误,谁能帮我一下
 
应该是COM+对象方法adoconn传出参数类型错误吧.
function TDatabaseConn.adoconn: olevariant;
//不适合以OleVarinat传给客户端
begin
Result := Fconn;
end;
试试这样用IDispatch接口传递吧.
function TDatabaseConn.adoconn: IDispatch;
begin
Result:=FConn as IDispatch;
end;
 
结果是一样的,还是有问题,
注意: showmessage(Conn.ConnectionString);
是正确
Conn.Execute('create table st1 (id integer)',RecordsAffected,1);/也可以执行
就是不能赋值
使用vc 调试问题一样,估计还是服务端的问题,快帮帮我
HRESULT hr;
_ConnectionPtr m_pConnection;
IDatabaseConn *pCom=NULL;

// 初始化COM
hr = CoInitialize(0);

// 使用SUCCEEDED 宏并检查我们是否能得到一个接口指针
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(
CLSID_DatabaseConn,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IDatabaseConn,
(void**) &amp;pCom);//生成接口调用类场
//pmaCom->ShowMa("",(WORD)GetSafeHwnd());//调用COM方法。
//pmaCom->ShowMa(NULL,(WORD)GetSafeHwnd());//调用COM方法。
m_pConnection=pCom->adoconn();
if (pCom==NULL)
{
AfxMessageBox("ok");
}
else
{
/*_bstr_t st="create table tt1 (id integer)";
VARIANT RecordsAffected;

m_pConnection->Execute(st,&amp;RecordsAffected, 0);//直接执行没有问题
*/
if (m_pConnection==NULL) return ;
_CommandPtr m_pCommand;
_bstr_t st="create table tttt (id integer)";
m_pCommand.CreateInstance("ADODB.Command");
_variant_t vNULL;
vNULL.vt = VT_ERROR;
vNULL.scode = DISP_E_PARAMNOTFOUND;///定义为无参数
m_pCommand->ActiveConnection = m_pConnection;///也是这里出错

m_pCommand->CommandText=st;
try
{
m_pCommand->Execute(&amp;vNULL,&amp;vNULL,adCmdText);///执行命令,取得记录集
return ;
}

catch(CException e)
{
AfxMessageBox("错误");
return ;
}
}
//pCom->Release();
CoUninitialize();
}

 
多人接受答案了。
 

Similar threads

I
回复
0
查看
620
import
I
I
回复
0
查看
604
import
I
I
回复
0
查看
775
import
I
I
回复
0
查看
654
import
I
后退
顶部