Solved

Database with ODBC problems

Posted on 1998-10-09
10
1,057 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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

12 Experts available now in Live!

Get 1:1 Help Now