Avatar of SteveBay
SteveBay
Flag for United States of America

asked on 

Hyphens in the name entered into DefaultDatabase property cause EOleException in TADOConnection

When I enter a Database Name in the DefaultDatabase property that contains a hyphen (i.e. My-Database) and then set the ADOConnection.Connected Property=true, I get Property value is invalid. Make sure the value is typed correctly. This is the same behavior you would see if you put in a database name that does not exist on the Server.
While I have found a work-around I would still like to know if anyone else has experienced this and and if so were you able to find the fix?
Test code and work-around shown below:

procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
     Memo1.Clear;
     qry.Close;
     ADOConn1.DefaultDatabase := '';
     ADOConn1.Connected := True;
     qry.Connection := ADOConn1;
 
     qry.SQL.Clear;
     qry.SQL.Add('Exec SP_DATABASES');
     try
          qry.Active := True;
          while not qry.Eof do
               begin
               CheckDBName( qry.FieldByName('DATABASE_NAME').AsString );
               qry.Next;
               end;
          qry.Close;
     except
          on e : exception do begin
          ShowMessage('Error getting Databases:' + e.Message);
          end;
     end
 
end;
 
 
function TForm1.CheckDBName(aName : String): Boolean;
begin
     Result := True;
     try
          if chkUseWorkAround.Checked then
               ChangeDefaultDB(ADOConn2, aName) //<< This is my work-around
          else
               ADOConn2.DefaultDatabase := aName; // << This will throw an exception
 
          Memo1.Lines.Add(Format('Good:  %s   %s', [ADOConn2.DefaultDatabase, aName]));
          qry2.Close;
          qry2.SQL.Clear;
          qry2.SQL.Add('EXEC SP_TABLES @table_owner = ''dbo''');
          qry2.Active := True;
          Result := (not qry2.Eof);
          qry2.Active := False;
     except
          on e : exception do begin
               Memo1.Lines.Add(Format('Bad:  %s   %s', [ADOConn2.DefaultDatabase, aName]));
               Memo1.Lines.Add(' Error:' + e.Message);
               result := false;
               end;
     end;
 
end;
 
 
function TForm1.ChangeDefaultDB(aADOConnection : TADOConnection  ; aNewDBName : string) : Boolean;
var s1, s2: string;
     p : integer;
     SaveConnected :  Boolean;
begin
     // Remove Initial Catalog from connection string and
     // replace it with new Database name.
     // Unfortunatly we have to disconnect to do this.
     SaveConnected := aADOConnection.Connected;
     aADOConnection.Connected := False;
     s1 := aADOConnection.ConnectionString;
     p := pos('Initial Catalog=',s1);
     if (p <> 0) and (length(s1) > p + 16) then
          begin
          s1 := copy(s1,1 ,p -1);
          s2 := copy(aADOConnection.ConnectionString,p,length(aADOConnection.ConnectionString));
          p := pos(';',s2);
          if p = 0 then
               aADOConnection.ConnectionString := s1 + ';Initial Catalog=' + aNewDBName
          else begin
               aADOConnection.ConnectionString := s1 + copy(s2,p +1,length(s2)) + ';Initial Catalog=' + aNewDBName;
               end;
          end
     else
          aADOConnection.ConnectionString :=  aADOConnection.ConnectionString + ';Initial Catalog=' + aNewDBName;
 
     Result := True;
     try
          aADOConnection.Connected := SaveConnected;
     except
          Result := False;
     end;
end;

Open in new window

Editors IDEsDelphi

Avatar of undefined
Last Comment
Geert G

8/22/2022 - Mon