Solved

Database with ODBC problems

Posted on 1998-10-09
10
1,135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 clear a vector as well as how to detect empty vectors in C++.

690 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