Solved

use ADO database connections

Posted on 2001-06-25
7
326 Views
Last Modified: 2011-09-20
Hi!
I'm trying to connect to a database (ODBC) using ADO. I've found a example on the 'NET' but it doesn't seem to work. My plan is to use ADO to open an connecton and then save a recordset in XML format. Here is my code to connect :

//Create the connection object
TCOM_Connection cn = CoConnection::Create();

//Use the ODBC DSN 'NUKLEUS' Access database:
cn->ConnectionString = WideString("DSN=NUKLEUS").Detach();
cn->CursorLocation = Adodb_tlb::adUseClient;
cn->ConnectionTimeout = 7;

//Pass 0 in all values to not change the connection string
//already defined previously
cn->Open(0,0,0,0);

//Do your SQL statement
WideString sql = "SELECT * FROM Products ";

//Create your recordset
TCOM_Recordset rs = CoRecordset::Create();
//Pass the sql statement, the connection, Cursor Type and the Lock Type
//Check the ADO documentation for more information
rs->Open(sql.Detach(), (IDispatch *)cn, Adodb_tlb::adOpenDynamic,
         Adodb_tlb::adLockOptimistic, 0);
    // if no entry in the table
    if(rs->BOF == true && rs->EOF == true)
    {
    MessageBox(NULL, "No records found matching your criteria", "Query", MB_ICONSTOP);
    }

    Memo1->Lines->Append("Records found: " + String(rs->RecordCount) );

    //Close the recordset
    rs->Close();
    //Release the memory used by the recordset
    rs->Release();
    //Close the connection
    cn->Close();
    //Release the memory used by the database connection
    cn->Release();

If anyone have hade this same problem or has an solution please msg me.

/wr Henrik
0
Comment
Question by:swingfly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6224106
What error message are you seeing?  What is the problem?  You might like to post a link to this question in the MFC topic area.
0
 
LVL 4

Expert Comment

by:rdov
ID: 6224175
swingfly,
do you wish to use MFC objects (CDatabase,CRecordset)?
0
 

Author Comment

by:swingfly
ID: 6224963
Yes, i dont have ADBExpress.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
rdov earned 100 total points
ID: 6226554
1. first add ODBC Data Source using SQLConfigDataSource function:

char  attribBuffer[MAX_GET_STR];
int   j;
j  = sprintf( attribBuffer, "DSN=DSN_NAME\0");
j++; //don't forget to leave the null
j += sprintf( attribBuffer + j, "DBQ=%s\0\0", szAttributes);
j += 2; //don't forget to leave the double-null
//configure data source "dbConvertSource"-Alias
int iRetcode=::SQLConfigDataSource(
NULL, //disable dialog box
ODBC_ADD_DSN, //add a data source
sDriver,
attribBuffer //attributes, see above
);
// or just write:
SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)","DSN=DSN_NAME\0DESCRIPTION=SQLConfigDSN

DBImport\0DBQ=c:\\Directory Name\\FileName.mdb\0");


2. then create a CDatabase Object and exe a SQL query:
    CDatabase dbImport;
    try
    {
         if( !dbImport.IsOpen( )  &&
         dbImport.OpenEx( _T( "DSN=DSN_NAME;" ),
                    CDatabase::openReadOnly |
                    CDatabase::noOdbcDialog ))
       {
              CString sSQLQuery="SELECT * FROM [" + m_sTableName + "]";
              CRecordset tblImport(&dbImport);
              if (tblImport.Open(CRecordset::snapshot,sSQLQuery))
              {
                   int iLineNumber=1;
                   while (!tblImport.IsEOF())
                   {
                        try
                        {
                             tblImport.GetFieldValue(iColumnIndex,sStrColumn);
                        }
                        catch (CDBException* pEx)
                        {
                             AfxMessageBox(pEx->m_strError);
                             pEx->Delete();
                             return FALSE;
                        }                                  
                        iLineNumber++;
                        tblImport.MoveNext();
                   }
                   tblImport.Close();
              }
         }
    }
    catch (CDBException* pEx)
    {
         AfxMessageBox(pEx->m_strError);
         pEx->Delete();
         return FALSE;
    }

for more information on CDatabase/CRecordset object you can find in the MSDN (http://search.microsoft.com/us/dev/default.asp) or ask me.


Hope it helps...
0
 
LVL 4

Expert Comment

by:rdov
ID: 6316333
swingfly?
0
 
LVL 11

Expert Comment

by:griessh
ID: 7014572
Dear swingfly

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "rdov"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7030992
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question