|
Hi,
Your tutorial is excellent... Have you manage to modify you code with the ability to write data in the Test.mdb file. If not Can you show a tutorial too?
Hoping for positive response.
Roel123
|
|
|
|
|
thanks
i need it
and how can i connect to a sql or database (.mdf) file and query on that?
i'm working visual c 2010.
|
|
|
|
|
I included the Try/Catch blocks in the code. The program builds successfully. When i run, it runs successfully, displays the list control as expected. When i click the Read button. I get an exception. Am given the message Unhandled exception at 0X00414ea6 in ReadDB.exe : 0XC0000005 : Access violation reading location 0X00000001. At the bottom of the message box there is Break, Continue, Ignore buttons.
I click the Break button and a file atlsimpstr.h is opened. the yellow arrow for break points on the return statement in code fragment below.
CStringData* GetData() const throw()
{
return (reinterpret_cast<cstringdata*>(m_pszData)-1);
}
Please help me with this.
|
|
|
|
|
Thanks for the response. I have put the Try/Catch blocks on opening the database and on opening recordset CRecordset recset. Still i get the same problem. The program runs and displays the dialog box containing the List Control. When i click the Read button so that it reads data from the Database on to the List Control, instead it populates the list control with gridelines and doesnot show any data from the database. Its presents an empty List Control with no data from the database and no error messages. I will be glad for your help.
modified on Friday, March 19, 2010 5:26 PM
|
|
|
|
|
Seriously...why would you put that much code into a try/catch block? You need to target the statements that could throw an exception. A lot of the code within that Try/Catch will never throw an exception and if you do get an exception as a result of something within that code, it will be harder to figure out what your problem was.
|
|
|
|
|
Hi people, am new in VC++ programming and using Microsoft Visual C++.NET. I have run the program in the above article but it generated errors on building. I modified the function below as follows. ResetListControl()function remained as it was. It runs without errors but doesnot show the data in database. Please help.. I will be grateful
void CReadDBDlg::OnRead()
{
// TODO: Add your control notification handler code here
CDatabase database;
CString SqlString;
CString sCatID, sCategory;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "C:\\Documents and Settings\\Owner\\Desktop\\mfcdataAccess\\readDb\\ReadDB_demo\\Test.mdb";
int iRec = 0;
// Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL statement
SqlString = "SELECT CatID, Category "
"FROM Categories";
// Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Reset List control if there is any data
ResetListControl();
// populate Grids
ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
// Column width and heading
m_ListControl.InsertColumn(0,"Category Id",LVCFMT_LEFT,-1,0);
m_ListControl.InsertColumn(1,"Category",LVCFMT_LEFT,-1,1);
m_ListControl.SetColumnWidth(0, 120);
m_ListControl.SetColumnWidth(1, 200);
short nFields = recset.GetODBCFieldCount( ); //get total no. of fields
CDBVariant varValue;
// Loop through each record
while( !recset.IsEOF() )
{
for( short index = 0; index < nFields; index++ )
{
// Copy each column into a variable
recset.GetFieldValue( index, varValue );
switch ( index ) {
case 0: // field no 1
if ( varValue.m_dwType == 8 )
//Insert values into the list control
iRec = m_ListControl.InsertItem(0,varValue.m_pstring->GetBuffer(),0);
break;
case 1: // field no 2
if ( varValue.m_dwType == 8 )
m_ListControl.SetItemText(0,1,varValue.m_pstring->GetBuffer());
break;
}
}
// goto next record
recset.MoveNext();
}
// Close the database
recset.Close();
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
// Reset List control
void CReadDBDlg::ResetListControl()
{
m_ListControl.DeleteAllItems();
int iNbrOfColumns;
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
if (pHeader)
{
iNbrOfColumns = pHeader->GetItemCount();
}
for (int i = iNbrOfColumns; i >= 0; i--)
{
m_ListControl.DeleteColumn(i);
}
}
|
|
|
|
|
Hi all,
I got an error when querying like this
CString SqlString;
strSqlString.Format(_T("select * from IOData where HolderName = %s and IODate = %s"), strHolderName, mstrStartDate);
database.ExecuteSQL(SqlString);
Error shown is
"Syntax Error too mny arguments"
Please give me an idea about it
modified on Monday, June 30, 2008 2:35 AM
|
|
|
|
|
You need single quote on both side of %s e.g. '%s'
corrected:
strSqlString.Format(_T("select * from IOData where HolderName = '%s' and IODate = '%s'"), strHolderName, mstrStartDate);
Try this, this will work.
|
|
|
|
|
Hi all,
I got an error when querying like this
CString strStartDate;
strStartDate.Format(_T("#%s#"), strDate);
CString SqlString;
SqlString.Format("select * from IOData where IODate = strStartDate");
database.ExecuteSQL(SqlString);
Error shown is
"Syntax Error in date in query expression
IODate = strStartDate"
Please give me an idea about it
|
|
|
|
|
This is really helpful for a beginner. Could you please explain some basics on DSN?
Jes
|
|
|
|
|
Can any one please explain me how???
|
|
|
|
|
Hello,
To the test.mdb file, I added a new table. The name of the table is Makes. This table contains 3 fields: ID, CatID, and Make. In the code, I changed the value of SqlString to "SELECT Categories.Category, Makes.Make FROM Categories, Makes WHERE Categories.CatID=Makes.CatID". When I run the program, I get a message box with the following message: Database error: Type mismatch in expression. Any help is appreciated.
Mike
|
|
|
|
|
Please help me to create MS Access file with the help of mfc code(ODBC)
|
|
|
|
|
Hi,
Even I've used the same method.
How do I get the connection mentioned in main dlg box,apply to even modal dlgboxes.
My problem is Ican dynamically retrieve records in main dlgbox but failed to do so in modal dlgboxes.I've included main dlgbox header file,even then it doesn't work.
WHT could be the problem?
swapna_signsin
|
|
|
|
|
i am an engg.student of comp.science i need to make connectivity of c/c++ with access/sql database . i need the code that can run on a compiler which is common to use like dev-c++ etc
if u an help me then plz do
|
|
|
|
|
Hi, I was looked for what is offered by CDatabase in MS Access
even after adding the process.h still I get this Error
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/database.exe : fatal error LNK1120: 2 unresolved externals
Any Help would be appreciated. I am using this under Win32 API programming:
#include <afxdb.h>
#include <odbcinst.h>
#include <process.h>
{
....some other codes
}
void FillListBox (HWND hwndList)
{
CDatabase database;
CString SqlString;
CString sCatID, sCategory;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "c:\\test\\Test.mdb";
int iRec = 0;
// Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL statement
SqlString = "SELECT CatID, Category "
"FROM Categories";
// Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Loop through each record
while( !recset.IsEOF() )
{
// Copy each column into a variable
recset.GetFieldValue("CatID",sCatID);
recset.GetFieldValue("Category",sCategory);
/*
// I thought this SendMessage will work!
CString szFormatId, szFormatCat;
szFormatId.Format("%d",sCatID);
szFormatCat.Format("%s",sCategory);
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szFormatId) ;
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szFormatCat) ;
// But it give following error
// : error C2440: 'type cast' : cannot convert from 'class CString' to 'long'
// No user-defined-conversion operator available that can perform this conversion,
// or the operator cannot be called
*/
// goto next record
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
|
|
|
|
|
You need to include MFC. In the project options (I believe) there is an option on one of the tabs that says Include MFC in a Static Library. That button needs to be checked and then the project should compile.
Josh
|
|
|
|
|
Please add these header files and try, hopefully it will work.
#include "afxdb.h"
#include "odbcinst.h"
-- modified at 13:25 Monday 31st October, 2005
|
|
|
|
|
Hello,
It appears as soon as it gets to recSet.Open(...) I get this error :
"No Columns were bound prior to calling SQL FetchScroll/SqlExtendedFetch"
any idea what causing this ?
thanks
|
|
|
|
|
Sure, I also notice that CRecordset can not open with "Dynaset". This problem seems having no solution. This means you can not update the tables in Access through CRecordset. ODBC can not be used anymore?
|
|
|
|
|
i have done a mfc app, that lists all the datas of a particular database(MS Access) in a List control box... now i want to get the particular record when clicking on an items listed in List Control Box.... how can i do this ??
anybody please help.. if possible explain in details sir...
thanks in advance....
sakesh
|
|
|
|
|
GetFieldValue supports Unicode?
I have some unicode data in my database. After this function call ( GetFieldValue("Descript",sDescript) I get always "??????" characters. For debugging I have used
fout << (const char*) sDescript << endl;
tnx
|
|
|
|
|
You have to use UNICODE with the rest of your program, too, the string from the database doesn't get magically converted to ANSI.
Make sure sDescript is a CString.
GetFieldValue( _T("Descript"),sDescript);
fout << (LPCTSTR)sDescript << endl;
Better yet, use the MFC functions for file handling.
|
|
|
|
|
Can a MS Access database be password protected? And if so, how do deal with that when trying to read the database in your program?
|
|
|
|
|
Hi, please help.
I'm a novice coder and the database connection method identified on this page is practically exactly what i needed for my group's application. The following code is an attempt to connect to an access database on my hard drive, check a value in a certain field of a certain table, update that field if its value is 'no' and add data into some other fields. Here's the code
#include <stdio.h>
#include <stdlib.h>
#include <afxdb.h>
extern "C" {
#include <odbcinst.h>
}
void main()
{
CDatabase database;
CString sDriver = "Microsoft Access Driver (*.mdb)";
CString sDsn;
CString sFile = "c:\\WPS\\WPSData.mdb";
CString SqlString, SqlString1;
CString varValue;
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
database.Open(NULL,false,false,sDsn);
CRecordset recset(&database);
SqlString = "SELECT * FROM Member1234";
recset.Open(CRecordset::forwardOnly,SqlString);
while(recset.IsEOF()!=1)
{
recset.MoveNext();
}
recset.GetFieldValue("ParkCheck", varValue);
if(varValue = "No")
{
SqlString1.Format("UPDATE Member1234 SET [ParkCheck] = #%s#", "Yes");
database.ExecuteSQL(SqlString1);
SqlString1.Format("INSERT INTO (ParkID, Date, ParkInTime) VALUES (1, '7/04/2004', '8.30pm')");
database.ExecuteSQL(SqlString1);
if(!recset.Update())
{
AfxMessageBox("Record not added");
}
}
else
{
SqlString1.Format("INSERT INTO (ParkOutTime) VALUES ('8.30pm')");
database.ExecuteSQL(SqlString1);
if(!recset.Update())
{
AfxMessageBox("Record not added");
}
}
database.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
I get the following unresolved external linker errors and i have NO idea why??? There's 33 of them, so please bear with me
[Linker Error] Unresolved external '__stdcall CException::operator delete(void *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDBException::~CDBException()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CException::~CException()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall CObject::operator delete(void *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CObject::~CObject()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::~CString()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::~CDatabase()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CPtrList::~CPtrList()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall AfxTryCleanup()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::~CRecordset()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CMapPtrToPtr::~CMapPtrToPtr()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::CDatabase()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::CString(const char *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::CString()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'AFX_EXCEPTION_LINK::AFX_EXCEPTION_LINK()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::Open(const char *, int, int, const char *, int)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::CRecordset(CDatabase *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::MoveNext()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::IsEOF() const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::Open(unsigned int, const char *, unsigned long)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::CanAppend() const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::GetFieldValue(const char *, CString&)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::operator =(const char *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::Format(const char *, ...)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::ExecuteSQL(const char *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::Update()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::Close()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDBException::classCDBException' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CObject::IsKindOf(const CRuntimeClass *) const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall AfxAssertFailedLine(const char *, int)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall operator +(const char *, const CString&)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::operator const char *() const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall AfxMessageBox(const char *, unsigned int, unsigned int)' referenced from C:\WPS\LISTENING.OBJ
Any help or solution would be greatly appreciated. Thankyou in advance
|
|
|
|
|