Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 10-21-2016, 06:35 AM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default TADOQuery crashing on open in thread

What used to run fine is now crashing, and I can't seem to figure out why.
I have placed the TQuery (of type TADOQuery) in a try/except block, but it never gets to the except part.
When I single step it, I can get it all the way to a procedure in Data.Win.ADODB as per the below:

Code:
 
procedure TADOCommand.OpenConnection;
begin
  if not Assigned(CommandObject.Get_ActiveConnection) then
  begin
    if ConnectionString <> '' then
      CommandObject._Set_ActiveConnection(FConnectionString)
    else if Assigned(FConnection) then
    begin
      FConnection.CheckActive;
      CommandObject.Set_ActiveConnection(FConnection.ConnectionObject);
    end else
      DatabaseError(SMissingConnection);
  end;
end;
I can't get it further than the line
CommandObject._Set_ActiveConnection(FConnectionStr ing)

When I F7/single step it from this line, it goes to the procedure in Systems.Variants as per below:
Code:
 
procedure _VarFromWStr(var V: TVarData; const Value: WideString);
begin
  if (V.VType and varDeepData) <> 0 then
    VarClearDeep(V);
  V.VOleStr := nil;
  V.VType := varOleStr;
  WideString(Pointer(V.VOleStr)) := Copy(Value, 1, MaxInt);
end;
However, it processes each of these lines fine, gets to the final 'end', and then crashes, so I'm not sure how to find the reason.

I have previously had a problem like this, and someone said to include a
TQuery.ParamCheck := False;
which I have done, but it still crashes.

This is my procedure in the thread...

Code:
 
var
 TQuery : TADOQuery;
 query : String;
 i : Integer;
begin
 CoInitialize(nil);
 TQuery := TADOQuery.Create(nil);
 TQuery.ConnectionString := 'FILE NAME=c:\ProductionDB\MP2.udl';
 TQuery.ParamCheck := FALSE;
 query := 'SELECT empcode, firstname, lastname, socsecnum, hiredate, description, costcenter FROM emp ' +
          'LEFT JOIN dept ON emp.department = dept.department';
 TQuery.SQL.Clear;
 TQuery.SQL.Add(query);
 i := 0;
 try
  TQuery.Open;
 except
  i := 9; //used just so I can single step to this line
 end;
....
  #2  
Old 10-21-2016, 07:51 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,305
Default

Did you change the content of the .udl?
Since that is holding your real connection settings.

Try rebuilding your .udl

Other thing that I can think of is permissions on the .udl. Perhaps it's locked exclusively somewhere. Rebuilding it would also solve this issue (in the short run). If that's the case you still should figure out what is locking it (perhaps some test code, it's open in an editor, or whatever you could think of)

Is all this still not working try giving the connectionstring directly in the AdoQuery.ConnectionString (database, user, and so on that is now configured in your udl). Check if that's working properly, perhaps there's something wrong there (db changed permissions on user or whatever).
  #3  
Old 10-21-2016, 08:08 AM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default

I haven't changed the UDL file at all, still the original one.
And I use this UDL in other programs too.
But this is its contents anyway, with user/pw removed...

Code:
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=...;Password=...;Initial Catalog=MP2;Data Source=192.168.1.4\sqlexpress
To check if some process had it locked, I made a copy of it, deleted the original, and renamed the copy to original name again, still same issue.
  #4  
Old 10-21-2016, 09:52 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,305
Default

And you've provided the AdoQuery connectionstring with this one as suggested?

Furthermore, naming a variable TQuery is also not a good idea. Since TQuery is also a class in DBTables.
I would refactor it to:
Code:
 query: TADOQuery;
 sql: String;
And if all this still doesn't work, what happens if you create a TAdoConnection and set it to Connected, does that work?
And if that works, instead of the ConnectionString assign the Connection with this new AdoConnection.

Just figuring out if it goes wrong in the connection or in some other part.
For example executing the query, but that would normally give an exception, as connection failure should do also. Therefor my refactoring remark (it's a real longshot though)
  #5  
Old 10-21-2016, 10:16 AM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default

Thanks Norrit for your efforts trying to help me.
So I tried to manually set the connection string for the ADOQuery, still crashes.
Then I also changed the TQuery to query, and the query to str as suggested, same result.
Lastly I tried the ADOConnection, it hangs for about 15 seconds trying to assign the connection string, then crashes (before I even set it to connected).
I then tried setting the connection string to a different database (MySQL, the first being an MSSQQ), same result.

The strange thing is that I use this exact setup in my other threads and it works, This one also used to work. I was just wanting to debug it as it wasn't giving me the results I was expecting, and now it just doesn't get past this point.
  #6  
Old 10-21-2016, 12:05 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,305
Default

And that's the only code in this thread?
What might be an issue is that you call this code from another part where you also did CoInitialize...

Then you could get a construction like:
Code:
  CoInitialize(nil);
  try
    ... do some things
    DoSomething();
    ... do some more things
  finally
    CoUninitialize();
  end;

procedure DoSomething();
begin
  CoInitialize(nil);
  try
    // bla...
  finally
    CoUnitialize();
  end;
end;
If you reach the "... do some more things" you've called CoInitialize(nil) twice, called CoUnitialize (which has no clue of what to unitialize so all will be done).
And I'm not sure what calling CoInitialize(nil) twice has for an effect???

But again, another longshot...
  #7  
Old 10-24-2016, 05:49 AM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default

I have found the problem, and it was being caused from the main program!
I have a lot of threads being called from the main program, so in testing this thread, not sure what I thinking when I wrote the code below, but I was calling the procedure that set up the thread, and then told the application to shutdown!
This is what I had...

Code:
 
  Birthdays();
  Application.Terminate;
  exit;
Obviously it just needed the exit, not the Appllication.Terminate;

Norrit, thanks for pointing me back to the main program, even if it wasn't the CoInitialize that was the problem. Just to answer your question though, I only call these threads once at a time, and if it is still busy, I don't try calling it again, so there will never be a time when CoInitialize has been invoked more than once per thread.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:00 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.