Solved

use ADO database connections

Posted on 2001-06-25
7
315 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IE call stack entries where function call address is not in a module 4 62
C++ get user from AD  (VS6) 7 55
thread-safe code in c++ 2 86
Why isn't object file created? 6 43
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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now