CRecordSet
-
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
-
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
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.