Solved

use ADO database connections

Posted on 2001-06-25
7
329 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
Industry Leaders: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

624 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