Solved

Database with ODBC problems

Posted on 1998-10-09
10
1,094 Views
Last Modified: 2010-05-18
Hi experts,
here is a easy question for people that have already used ODBC :
Well I try to get some Data from a Database (Access), but I don't know what's wrong in my code. So have a look and tell me the mistake. Thank you

Code:

void CDBTestDlg::OnOpen()
{
      TCHAR* sql="SELECT * FROM Package";
      // open the Database browser
      cdb.Open( NULL, FALSE, TRUE );
      crs = new CRecordset( &cdb );
        crs->Open( AFX_DB_USE_DEFAULT_TYPE, sql, CRecordset::readOnly );
      //AFX_ODBC_CALL( ::SQLExecDirect( crs->m_hstmt, reinterpret_cast<UCHAR FAR*> (sql) , 22) );
      nRetCode = ::SQLFetch( crs->m_hstmt );
}

void CDBTestDlg::OnNext()
{
      UCHAR      szPackage[50], szPkgID[50];
      SDWORD      cbPackage, cbPkgID;
      CString cstr;
      
if ( nRetCode  != SQL_NO_DATA ) {
            AFX_ODBC_CALL( ::SQLGetData( crs->m_hstmt, 1, SQL_C_CHAR, szPackage, 50, &cbPackage) );
            AFX_ODBC_CALL( ::SQLGetData( crs->m_hstmt, 2, SQL_C_CHAR, szPkgID, 50, &cbPkgID) );
      }
      m_Edit.SetWindowText( (char *) &szPackage );
      m_Edit2.SetWindowText( (char *) &szPkgID );
}


The problem is, when I open the CRecordset, the following message appears :
"No Columns were bound prior to call SQLFetchScroll/SQLExtendedFetch"
0
Comment
Question by:jufer
  • 6
  • 3
10 Comments
 

Expert Comment

by:ofer1
ID: 1174732
Try to put a ";" in you SQL statement. like that:
TCHAR* sql="SELECT * FROM Package;";
0
 

Author Comment

by:jufer
ID: 1174733
Well it's not that!
If I use SQLGetData it works: I got the data and I can move to the next Record with SQLFetch. But I want to use the memberfunction of CRecordset to get the next Recordset (MoveNext(), MovePriv(), MoveFirst,... ). So now I see, that I mixed SQLExecDirect, SQLGetData with the class CRecordset. CRecordset has also memberfunction that get the data and I don't have to use SQL-function. So perhaps you can help.

Thank you
Jufer
0
 

Author Comment

by:jufer
ID: 1174734
So the point I want to know is :
I'm looking for an easy Database access (Open the Database, make a Recordset and get some Data, close the Database) and I want to use CRecordset's memberfuntion to get the Data (crs.MoveNext, crs.MovePriv,...) An Codeexample would be nice.

cheers
jufer
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:jufer
ID: 1174735
Well perhaps there are other experts that can explain me, what it's wrong. The ";" at the sqlstring wasn't the problem.
Once again :
I want to use a database with odbc and I want to scroll to the next (or previous) recordset with the class CRecordset. I try to program the access like this
Open the Database : CDatabase cd.Open(NULL, FALSE, TRUE);
Create a CRecordset object: CRecordset crs(&cd);
Open the CRecordset with an SQLstatements crs.Open(CRecordset::snapshot, "SELECT * FROM TTable;");
Move : crs.MoveNext();
or so.
Note : this is not the code I programed (look above for the code), it's just pseudocode of my idea. Thanks for helping me

cheers,
jufer
0
 

Author Comment

by:jufer
ID: 1174736
Well perhaps there are other experts that can explain me, what it's wrong. The ";" at the sqlstring wasn't the problem.
Once again :
I want to use a database with odbc and I want to scroll to the next (or previous) recordset with the class CRecordset. I try to program the access like this
Open the Database : CDatabase cd.Open(NULL, FALSE, TRUE);
Create a CRecordset object: CRecordset crs(&cd);
Open the CRecordset with an SQLstatements crs.Open(CRecordset::snapshot, "SELECT * FROM TTable;");
Move : crs.MoveNext();
or so.
Note : this is not the code I programed (look above for the code), it's just pseudocode of my idea. Thanks for helping me

cheers,
jufer
0
 
LVL 1

Accepted Solution

by:
msa092298 earned 100 total points
ID: 1174737
You have a very easy way (but you will have to do a little recoding and use the odbc wizard).  Press Ctrl-w to bring the class wizard.  Press button "Add Class" then new.  Give your class a name, and from the combo box select CRecordSet. Click on "Create" you will get a dialog asking you for an ODBC source name from a combo box(you must have defined an ODBC source for your table).  Make sure the Bind All Columns check box is checked.  Finally select the table(s) your record set will depend upon then Ok,Ok.  You have your class there with data members mapping for each field in the table(s).  You may to modify the parameters for the record set to sort the results or filter them in GetDefaultSQL (you will find it in the created files).  You may need to go to Ctrl-W then "Member Variables" tab to select which fields(columns) you want to have member variables in the class for.  You can bind all fields then remove the fields you don't want to use. You can AddVariable or DeleteVariable as you want.  Or you can refresh the class wizard about the table structure if it changes by "Update Columns".  It is that easy!  You don't need ExecSQL at all!  Hope I helped you.
0
 

Author Comment

by:jufer
ID: 1174738
Well
sounds nice. But I don't want to show the data. I just want to make some SQL (get some Data) an work with the data intern. Is it still this way I have to go ?
Cheers
jufer
0
 
LVL 1

Expert Comment

by:msa092298
ID: 1174739
Explain please.  Do you mean dynamic binding?  (i.e. you know nothing about the structure of the table during programming time?  If the table structure is known at development time, then my answer is what you need.  However, if you are going into the hell of dynamically binding to an unknown ODBC source, then believe me, it is a long story with a lot of work to do.  I think (from your source) you don't need run time binding to an unknown table structure.  Also, what do you mean by getting some data and working with the data intern?
0
 

Author Comment

by:jufer
ID: 1174740
msa,
I mean (work intern) that I don't show what SQL returns to the user. So thank's a lot for your help and I try it later.
cheers
jufer
0
 
LVL 1

Expert Comment

by:msa092298
ID: 1174741
That's what you alread have with my answer.  A C++ class which is derived from CRecordSet and you can use it the same way you wanted.  It contains member variables which are mapped to table fields.  When you open the record set, you get the field values into the member variables.  MoveNext or Movewhatever and the member variables are filled for you with the data.  Remeber to check for EOF and BOF conditions.  And be prepared that most CRecordSet members may generate exceptions when called.  You will need to surround most of your calls to CRecordSet with TRY {...} CATCH (CMemoryException,e){...} ANDCATCH (CDBException) {...} ANDCATCH (CEXCEPTION){...}ENDCATCH.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

839 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