Article View: borland.public.cppbuilder.database.ado
Article #2209Using ADO with BCB4
From: Chip Ling
Date: Wed, 15 Oct 2003 21:24
Date: Wed, 15 Oct 2003 21:24
94 lines
3724 bytes
3724 bytes
Dear all,
I'm a BCB4 user, I currently found an article on the web saying I can
connect to any ADO DB by using BCB4.
Here is the URL
http://www.rpiconsultants.com/software/Articles/ADOBCB4/ADOBCB4.asp
So I do it a try. But got stuck on one of the code with a user defined
data type called FieldPtr. Anyone can help?
Here is what I did:
1. Import Type Library "Microsoft ActiveX Data Objects 2.0 Library
(Version 2.0)" to folder c:\Program Files\Borland\CBuilder4\Imports as
ADODB_TLB.cpp.
2. Include Unit Hdr ADODB_TLB.h to the main form.
#include <vcl.h>
#pragma hdrstop
#include "ADOTEST.h" //my main form cpp
#include "ADODB_TLB.h" //new include header
3. Place an edit box called txtPubId to the main form.
4. Place a command button called cmdAdoQuery to the main form.
5. Place a list box called lstTitles to the main form.
6. Go to control panel ODBC and set up the System DSN = AccessTestDB and
point to the Biblio.mdb
7. At the cmdAdoQuery button click event enter the following code.
void __fastcall TForm1::btnADOQueryClick(TObject *Sender)
{
//Create the connection object
TCOM_Connection cn = CoConnection::Create();
//Use the ODBC DSN 'AccessTestDB' Access Database Biblio.mdb
cn->ConnectionString = WideString("DSN=AccessTestDB").Detach();
cn->CursorLocation = Adodb_tlb::adUseClient;
cn->ConnectionTimeout = 7;
//Pass 0 in all value to not change the connection string
cn->Open(0,0,0,0);
//Prepare the SQL statement
WideString sql = "SELECT Title FROM Titles ";
sql += "WHERE PubId = " + txtPubId->Text.Trim();
lstTitles->Items->Clear();
//Create the recordset
TCOM_Recordset rs = CoRecordset::Create();
rs->Open(sql.Detach(), (IDispatch *) cn, Adodb_tlb::adOpenDynamic,
Adodb_tlb::adLockOptimistic, 0);
//Check for empty result set
if (rs->BOF == true && rs->EOF == true)
{
MessageBox(NULL, "No records found matching your criteria",
"Query", MB_ICONSTOP);
}
else
{
// Reference a field object to the Title Field
FieldPtr p = rs->Fields->get_Item(TVariant("Title")); // this
line gives me error message when compile
// Get the field value (Title name)
String s = p->get_Value();
this->lstTitles->Items->Add(s);
}
//Close the recordset
rs->Close();
//Close the connection
cn->Close();
}
// end of code
I got a compilation error saying that [C++ Error] ADOTEST.cpp(53): E2451
Undefined symbol 'FieldPtr'.
I do a test by comment out the whole else block and then compile and
run. Then enter a impossible number on the txtPubID, and I get a not
found message box. Looks like the whole thing is working except I must
have missing some of the include file.
Any Help?
Rgds,
Chip Ling
Technical Details
Message-ID:
<3F8DBB08.3C297236@yahoo.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10698b,60d8269e6d9da7f9,start X-Google-Attributes: gid10698b,public X-Google-ArrivalTime: 2003-10-15 14:39:46 PST Message-ID: <3F8DBB08.3C297236@yahoo.com> Date: Wed, 15 Oct 2003 17:24:24 -0400 From: Chip Ling <chip_ling_vb@yahoo.com> X-Mailer: Mozilla 4.72 [en] (WinNT; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.database.ado Subject: Using ADO with BCB4 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 204.101.58.41 X-Trace: newsgroups.borland.com 1066252921 204.101.58.41 (15 Oct 2003 14:22:01 -0700) Lines: 96 Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail