ReOpen ADO Recordset with a current Connection

ReOpen ADO Recordset with a current Connection

Post by Andrew Smit » Tue, 14 Jan 2003 18:54:11



Hi

I have the following problem. I would like to ReOpen a
Recordset with pRs->Open(...) using a current Connection
pCon which I used before for another Recordset.

How can I do this?
Thanx, Andrew

----- Code ------
#import "C:\Program Files\Common
Files\SYSTEM\ADO\msado15.dll" rename("EOF","AdoEof")
#include <iostream>
using namespace std;

void PrintComError(const _com_error &e)
{
        // Notify the user of errors if any.
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());

         // Print Com errors.
        printf("Error\n");
        printf("\tCode = %08lx\n", e.Error());
        printf("\tCode meaning = %s", e.ErrorMessage());
        printf("\tSource = %s\n", (LPCSTR) bstrSource);
        printf("\tDescription = %s\n", (LPCSTR)
bstrDescription);

Quote:}

int main(int argc, char* argv[])
{
        if( FAILED(::CoInitialize(NULL)) )
        {
                cout << "CoInitialize faild!" << endl;
                return 1;
        }

        try
        {
                ADODB::_CommandPtr pCmd(__uuidof
(ADODB::Command));
                ADODB::_ConnectionPtr pCon(__uuidof
(ADODB::Connection));
                _bstr_t bstrConn = "Provider=SQLOLEDB;
Data Source=PRODSRV1; Initial Catalog=TXAC; User Id=sa;
Password=;";
                _bstr_t bstrSql = "SELECT * FROM tLog";

                cout << "Open ADO Connection" << endl;
                pCon->Open(bstrConn, "", "",
ADODB::adConnectUnspecified);
                pCmd->ActiveConnection = pCon;

                pCmd->CommandType = ADODB::adCmdText;
                pCmd->CommandText = bstrSql;

                cout << "Open Recordset 1" << endl;
                ADODB::_RecordsetPtr pRs(pCmd->Execute
(NULL, NULL, ADODB::adOpenUnspecified));

                while (pRs->AdoEof != VARIANT_TRUE)
                {
                        string strConv;
                        _bstr_t bstrConv;
                        VARIANT vLogText = pRs->Fields-

Quote:>GetItem("LogText")->Value;

                        bstrConv = (_bstr_t)vLogText;
                        strConv = (string)bstrConv;
                        cout << strConv << endl;
                        pRs->MoveNext();
                }
                cout << "Close Recordset 1" << endl;
                pRs->Close();

                cout << "Open Recordset 2" << endl;
                //-----------------------------------------
-------
                // Problem is here!
                // How can i use the Active Connection to
ReOpen a Recordset for a Query?
                //-----------------------------------------
-------
                // This generates ERROR!
                pRs->Open("SELECT * FROM tLogAppX", pCon,
ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, NULL);
        // ERROR!

                while (pRs->AdoEof != VARIANT_TRUE)
                {
                        string strConv;
                        _bstr_t bstrConv;
                        VARIANT vLogText = pRs->Fields-

Quote:>GetItem("LogText")->Value;

                        bstrConv = (_bstr_t)vLogText;
                        strConv = (string)bstrConv;
                        cout << strConv << endl;
                        pRs->MoveNext();
                }
                cout << "Close Recordset 2" << endl;
                pRs->Close();

                cout << "Close ADO Connection" << endl;
                pCon->Close();
        }
        catch(const _com_error& e)
        {
                PrintComError(e);
        }

        CoUninitialize();
        return 0;

Quote:}

  AdoRsProblem.cpp
2K Download
 
 
 

ReOpen ADO Recordset with a current Connection

Post by Tian Min Hua » Wed, 15 Jan 2003 21:20:07


Hi,

The problem is the parameter for ActiveConnection was not set properly.
Please use pCon.GetInterfacePtr() instead of pCon as shown in the following
line:

pRs->Open("SELECT * FROM tLogAppX", pCon.GetInterfacePtr(),
ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, NULL);

Please check it on your side.

Have a nice day!

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

ReOpen ADO Recordset with a current Connection

Post by Andrew Smit » Tue, 21 Jan 2003 18:55:43


hi HuangTM

Perfect! Didn't know this until now.
You helped me a lot. Thank you!

Regards,
Andrew

Quote:>-----Original Message-----
>Hi,

>The problem is the parameter for ActiveConnection was not
set properly.
>Please use pCon.GetInterfacePtr() instead of pCon as

shown in the following
Quote:>line:

>pRs->Open("SELECT * FROM tLogAppX", pCon.GetInterfacePtr
(),
>ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, NULL);

>Please check it on your side.

>Have a nice day!

>Regards,
>HuangTM
>This posting is provided "AS IS" with no warranties, and
confers no rights.

>.

 
 
 

ReOpen ADO Recordset with a current Connection

Post by Tian Min Hua » Wed, 22 Jan 2003 21:44:41


Hi Andrew,

Thanks a lot for your feedback. You are welcome! :-)

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

ReOpen ADO Recordset with a current Connection

Post by mike » Thu, 23 Jan 2003 00:10:57


Quote:>-----Original Message-----
>Hi Andrew,

>Thanks a lot for your feedback. You are welcome! :-)

>Regards,
>HuangTM
>This posting is provided "AS IS" with no warranties, and
confers no rights.

>.

I am unable to get it to compile. This is the error for
the "cout << strConv << endl;" lines:
error C2679: binary '<<' : no operator defined which takes
a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
st
d::allocator<char> >' (or there is no acceptable
conversion)

Am I missing a reference?

 
 
 

1. missing bookmark property when reopening connection/recordset

using:

MS SQL 7.0
VB 6.0 SP3

everything works fine:

    dataenvironment.connection.open
    dataenvironment.command
    set datagrid1.datasorce = dataenvironment.rscommand

command is
    table, dynamic, server-side cursor, lock optimistic

if i reopen the connection ...

    dataenvironment.connection.open
   ..... have to close connection for some reason ...
    dataenvironment.connection.close
    dataenvironment.connection.open
    dataenvironment.command
    set datagrid1.datasorce = dataenvironment.rscommand

... the dataenvironment.rscommand recordset has no bookmark-property
BOF and EOF are false and table entries are valid

same thing happens if opening the connection fails (eg wrong servername),
opening in a second try works but again recordset has no bookmark property

any tips for me ?

thanks,
 Frank

2. DTS + Import + Data + From + Remote + SQL + Server

3. Resorting an Ado recordset without reopening it.

4. Database backups & the transaction log

5. About ADO and DE Connections reopening

6. SQL Query Anlayser..

7. HELP: Closing and ReOpening ADO Connection Causes Unspecified error

8. VB Combo Box problem

9. Recordset ReOpen create a new recordset ptr?

10. Collection of ADO Recordsets and/or ADO Connections

11. Leave recordset open, or close and reopen.

12. Filter after reopen recordset

13. Reopen the recordsets