Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Database connection through MFC help

Database connection through MFC help

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpc++databasequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Danberg
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • M Mike Danberg

      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

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • M Michael P Butler

        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

        M Offline
        M Offline
        Mike Danberg
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups