Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

use ADO database connections

Posted on 2001-06-25
7
Medium Priority
?
340 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
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
Technology Partners: 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

927 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