Solved

use ADO database connections

Posted on 2001-06-25
7
312 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
Comment Utility
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
Comment Utility
swingfly,
do you wish to use MFC objects (CDatabase,CRecordset)?
0
 

Author Comment

by:swingfly
Comment Utility
Yes, i dont have ADBExpress.

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Accepted Solution

by:
rdov earned 100 total points
Comment Utility
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
Comment Utility
swingfly?
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

771 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

15 Experts available now in Live!

Get 1:1 Help Now