2

I have the following code wich is raising an EConvertError - "can not assign TAdoConnection to TAdoConnection"

var
  wTmpADOConn       : TADOConnection;
begin
  //
  Result := nil;
  try
    Result := TADOConnection.Create(nil);
    wTmpADOConn := afunc(aNameConn);//aFunc-Function which returns a pointer to a  TADOConnection object, aNameConn - string- the name of connection
    if wTmpADOConn <> nil then
    begin
      Result.Assign(wTmpADOConn);//here the error is raising

I can not understand what I'm doing wrong. I've tried to cast to TAdoConnection or TPersistent the variable wTmpADOConn, but without success.

I know that for copying an object it can be used the Assign procedure.

Code is written in Delphi 7.

LE: thank you all, the problem is that the object is a shared object, is a reference to another ADOConnection. And I want my own object so i can free it, without freeing the reference. The logic is complex, and I need some mechanism to create an object from that reference.

RBA
  • 12,337
  • 16
  • 79
  • 126
  • 1
    As said, you cannot "assign" a live connection, but in most cases you can create a new connection based on the connection string of the shared object to create a new connection to the same database. It maybe implies doubling your db-client-licensing fees, tough. – jachguate Oct 26 '10 at 17:50

4 Answers4

2

You can't assign TADOConnection to TADOConnection because such an assignment is not implemented in TADOConnection class. That is quite common - a lot of classes do not implement Assign method, and you can't expect that any TPersistent descendant implements the Assign method - usually there is no need in it.

kludg
  • 27,213
  • 5
  • 67
  • 118
1

Where does wTmpADOConn reference come from? If it is from a different executable module (dll) and no packeges is used, the types of wTmpADOConn and Result will not match.

Igor
  • 15,833
  • 1
  • 27
  • 32
1

TAdoConnection (like other "connection" classes) is not just a block of assigned memory. Usually it contains handles or other state-information to active resources, like a open connection to a database, open files, and such.

And the connections are usually not designed to share this "live" connection among different instances.

Anyway, if afunc returns a newly created connection and not a shared object, you can just return the afunc created one. If afunc return nil, create a new one, like this:

var
  wTmpADOConn       : TADOConnection;
begin
  Result := afunc(aNameConn);//aFunc-Function which returns a pointer to a  TADOConnection object, aNameConn - string- the name of connection
  if Result <> nil then
    Result := TADOConnection.Create(nil);

of course, your logic may be complex... I'm just playing with what you showed from your function...

Another way to do this is to re-use the parameters of the aFunc returned connection and open a new one...

Best regards.

jachguate
  • 16,976
  • 3
  • 57
  • 98
0

based on the answers (especially jachguate), I resolved it by assigning the connection string. code bellow

  Result.ConnectionString:=wTmpADOConn.ConnectionString;
  Result.LoginPrompt:=wTmpADOConn.LoginPrompt;
  Result.Name:=wTmpADOConn.Name;

+1 for all the answers. Thank you

best regards,

RBA
  • 12,337
  • 16
  • 79
  • 126