ADO _Recordset close problem...

This is a discussion on ADO _Recordset close problem... within the C++ Programming forums, part of the General Programming Boards category; I am developing a client server application which will transfer an Access MDB file from the backend server to a ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059

    ADO _Recordset close problem...

    I am developing a client server application which will transfer an Access MDB file from the backend server to a remote client. The server is run as a service. Everything works fine EXCEPT when I add records to the MDB file and then close the file. The close statement has a tendency to crash the server. I've tried googling on the close statement but couldn't find anything relating to problems with it. Listed below is the code fragment for your review. The recordset->Close() statement is the source of the problem. Any help would be greatly appreciated.

    Thanx

    Bob
    Code:
    ADODB::_ConnectionPtr connection;
    hr = connection.CreateInstance(__uuidof(ADODB::Connection));
    if (FAILED(hr))
    {
        throw _com_error(hr);
    }
    ADODB::_RecordsetPtr recordset;
    hr = recordset.CreateInstance(__uuidof(ADODB::Recordset));
    if (FAILED(hr))
    {
        throw _com_error(hr);
    }
    _bstr_t strcnn("Provider='Microsoft.JET.OLEDB.4.0';"
        "Data source = ");
    strcnn += szDatabaseName;
    connection->Open((const char *)strcnn,"","",ADODB::adConnectUnspecified);
    memset(szInsert, 0, sizeof szInsert);       
    sprintf(szInsert, "INSERT INTO DELFILE3LOG VALUES ('%s','%s')",szTimeStamp,szLogRec);
    recordset->Open(szInsert, connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
    	ADODB::adLockReadOnly, ADODB::adCmdText);        
    
    	recordset->Close();
    
    	additional code.....

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059

    addendum....

    The error message I get from the close statement is "Operation is not allowed when the object is closed. I believe I resolved the problem by using the following if statement for the recordset close() statement:
    Code:
    if(recordset & recordset->State == ADODB::adStateOpen)
       recordset->Close();
    Thus, we only close the recordset when it is OPEN.

    I'm under the impression that the recordset would still be open after initial recordset open statement. But apparently this is not the case. Thus, we have to check to determine if the recordset is open prior to closing it. Can anybody explain this to me???

    Thanx

    Bob

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe an error occurred on recordset->Open, therefore the recordset isn't always open when you try to close it. If this is the case, maybe you should probably check for an error when you open the recordset.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I've wrapped the complete function in try/catch statements and the only anomalistic message I get is "Operation is not allowed when the object is closed" when I do NOT check for a closed condition on the recordset prior to my attempt to close the recordset. Evidently, the Open statement is working properly.

    I've been stress testing the back end server for over 20 hours now without encountering any problems. I'm now checking for a closed condition on the recordset prior to attempting to close it. This appears to solve the problem. I guess I'll never really know why I have to check for a closed recordset even though I just opened it for updates.

    Thanx
    Bob

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Without testing, I can only assume that as you are running an action query (INSERT INTO) as opposed to a query that returns results (SELECT), that the recordset doesnt return anything - therefore it isnt really in an open state after the call.

    I dont come up with this problem myself as I dont create a recordset for an action query - there's no point as it's a waste of resources. You have already created the connection object, so just call Connection::Execute with the SQL statement and that should be fine. This is a more logical way to work, and more efficient as you dont need to create the Recordset object

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Yes, that's right, I'm doing an INSERT INTO which causes me grief. I'm an old school C programmer who's accustomed to opening a file prior to doing anything with that file and then closing the file. I guess this "old dog" will have to learn new tricks with ADO.

    Thanx!!!!!

    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21