Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CRecordSet

CRecordSet

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Girish601
    wrote on last edited by
    #1

    Hi, some sample code for read data from xls file. CDatabase database; CString sSql; CString Name, Address; CString sDriver; CString sDsn; CString sFile = "c:\\kdr.xls"; // the file name. Could also be something like C:\\Sheets\\WhatDoIKnow.xls // Clear the contents of the listbox m_List.ResetContent(); // Retrieve the name of the Excel driver. This is // necessary because Microsoft tends to use language // specific names like "Microsoft Excel Driver (*.xls)" versus // "Microsoft Excel Treiber (*.xls)" sDriver = GetExcelDriver(); if( sDriver.IsEmpty() ) { // Blast! We didn´t find that driver! AfxMessageBox("No Excel ODBC driver found"); return; } // Create a pseudo DSN including the name of the Driver and the Excel file // so we don´t have to have an explicit DSN installed in our ODBC admin sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile); TRY { // Open the database using the former created pseudo DSN // sSql = "INSERT INTO demo (Name,Address) VALUES ('Nilesh','Pune')"; // database.ExecuteSQL(sSql); database.Open(NULL,false,false,sDsn); // Allocate the recordset CRecordset recset( &database ); // Build the SQL string // Remember to name a section of data in the Excel sheet using "Insert->Names" to be // able to work with the data like you would with a table in a "real" database. There // may be more than one table contained in a worksheet. sSql = "SELECT Name, Address " "FROM demo " "ORDER BY field_1"; // Execute that query (implicitly by opening the recordset) recset.Open(CRecordset::forwardOnly,sSql);//,CRecordset::readOnly); // m_List.AddString( Name + " --> "+Address ); // recset.Open(NULL,sSql,NULL); // Browse the result // while( !recset.IsEOF() ) // { // Read the result line recset.GetFieldValue("field_1",Name); recset.GetFieldValue("field_2",Address); // Insert result into the list // m_List.AddString( Name + " --> "+Address ); // Skip to the next resultline recset.MoveNext(); // } // Close the database database.Close(); } CATCH(CDBException, e) { // A database exception occured. Pop out the details... AfxMessageBox("Database error: "+e->m_strError); } END_CATCH; } problem occur after recset.Open(CRecordset::forwardOnly,sSql);//,CRecordset::readOnly); it is not enter into condition. go to catch and throw error Database error : To few parameters e

    L 1 Reply Last reply
    0
    • G Girish601

      Hi, some sample code for read data from xls file. CDatabase database; CString sSql; CString Name, Address; CString sDriver; CString sDsn; CString sFile = "c:\\kdr.xls"; // the file name. Could also be something like C:\\Sheets\\WhatDoIKnow.xls // Clear the contents of the listbox m_List.ResetContent(); // Retrieve the name of the Excel driver. This is // necessary because Microsoft tends to use language // specific names like "Microsoft Excel Driver (*.xls)" versus // "Microsoft Excel Treiber (*.xls)" sDriver = GetExcelDriver(); if( sDriver.IsEmpty() ) { // Blast! We didn´t find that driver! AfxMessageBox("No Excel ODBC driver found"); return; } // Create a pseudo DSN including the name of the Driver and the Excel file // so we don´t have to have an explicit DSN installed in our ODBC admin sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile); TRY { // Open the database using the former created pseudo DSN // sSql = "INSERT INTO demo (Name,Address) VALUES ('Nilesh','Pune')"; // database.ExecuteSQL(sSql); database.Open(NULL,false,false,sDsn); // Allocate the recordset CRecordset recset( &database ); // Build the SQL string // Remember to name a section of data in the Excel sheet using "Insert->Names" to be // able to work with the data like you would with a table in a "real" database. There // may be more than one table contained in a worksheet. sSql = "SELECT Name, Address " "FROM demo " "ORDER BY field_1"; // Execute that query (implicitly by opening the recordset) recset.Open(CRecordset::forwardOnly,sSql);//,CRecordset::readOnly); // m_List.AddString( Name + " --> "+Address ); // recset.Open(NULL,sSql,NULL); // Browse the result // while( !recset.IsEOF() ) // { // Read the result line recset.GetFieldValue("field_1",Name); recset.GetFieldValue("field_2",Address); // Insert result into the list // m_List.AddString( Name + " --> "+Address ); // Skip to the next resultline recset.MoveNext(); // } // Close the database database.Close(); } CATCH(CDBException, e) { // A database exception occured. Pop out the details... AfxMessageBox("Database error: "+e->m_strError); } END_CATCH; } problem occur after recset.Open(CRecordset::forwardOnly,sSql);//,CRecordset::readOnly); it is not enter into condition. go to catch and throw error Database error : To few parameters e

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      vcforums wrote:

      sSql = "SELECT Name, Address " "FROM demo " "ORDER BY field_1";

      I suspect that this is your problem. I see where you selected into the demo table the Name and Address but there is no mention of a "field_1". If the field that is in your SQL select statement does not exist, then you will get that "too few parameters" error. Make sure that all of the fields exist in the database that you are trying to read. Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups