Database connection through MFC help
-
Hi, I posted about this a few days ago, but didn't get anywhere with it. I have a MFC DLL that I wrote to access a database that I'm using with my program (also MFC). What I've been doing is in each function I have in the dll, I connect to the database, perform the operation I want and then disconnect. This worked perfectly fine. However, I decided it was a waste of processing time to connect and disconnect each time I accessed the database, so I wrote two functions called connect and disconnect in order to be able to hold onto the database connection for the life of the program. This is how I did it. CDatabase database; __declspec(dllexport) void Connect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); database.Open( "EmployeeManager" ); } __declspec(dllexport) void Disconnect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); database.Close(); } This seems to me that it would work without problems. However, when I try to close the program it causes an error that says "A Breakpoint has been reached" and it crashes. All I did was override OnClose and call disconnect before I close. I stepped through the debugger and it doesn't crash on disconnect, but right after the close is executed. There are no breakpoints anywhere in the program so I have no idea what's going on here. Can anyone help me out? Thanks, Mike
-
Hi, I posted about this a few days ago, but didn't get anywhere with it. I have a MFC DLL that I wrote to access a database that I'm using with my program (also MFC). What I've been doing is in each function I have in the dll, I connect to the database, perform the operation I want and then disconnect. This worked perfectly fine. However, I decided it was a waste of processing time to connect and disconnect each time I accessed the database, so I wrote two functions called connect and disconnect in order to be able to hold onto the database connection for the life of the program. This is how I did it. CDatabase database; __declspec(dllexport) void Connect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); database.Open( "EmployeeManager" ); } __declspec(dllexport) void Disconnect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); database.Close(); } This seems to me that it would work without problems. However, when I try to close the program it causes an error that says "A Breakpoint has been reached" and it crashes. All I did was override OnClose and call disconnect before I close. I stepped through the debugger and it doesn't crash on disconnect, but right after the close is executed. There are no breakpoints anywhere in the program so I have no idea what's going on here. Can anyone help me out? Thanks, Mike
Mike Danberg wrote: __declspec(dllexport) void Disconnect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); CDatabase database; database.Close(); } Here you are closing an unopened database, as CDatabase is local to the function. This makes no sense what so ever. If you are trying to close the database opened in Connect, then you will have to make database a global variable rather than being local to the two functions. Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space
-
Mike Danberg wrote: __declspec(dllexport) void Disconnect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); CDatabase database; database.Close(); } Here you are closing an unopened database, as CDatabase is local to the function. This makes no sense what so ever. If you are trying to close the database opened in Connect, then you will have to make database a global variable rather than being local to the two functions. Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space
Oops. I posted the wrong code. Sorry. That CDatabase database shouldn't be in there. I don't remember why I put it there, probably just so it would compile without a problem. Anyway the code I had, uses a global variable, and it was crashing on close. Looked like this. CDatabase database; __declspec(dllexport) void Connect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); database.Open( "EmployeeManager" ); } __declspec(dllexport) void Disconnect() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); database.Close(); } Mike